Skip to content

Commit 8258d82

Browse files
committed
Merged PR 45442: Add platform for trackEvents to playground
Add platform for trackEvents to playground For each session ID, tracking the activity on playground by sending data to the telemetry
1 parent a441afa commit 8258d82

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

demo/v2-demo/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@
5959

6060
<script src="scripts/codesamples.js"></script>
6161

62+
<script src="scripts/aisdk.js"></script>
6263
<script src="scripts/index.js"></script>
6364
<script src="scripts/utils.js"></script>
65+
<script src="scripts/assert.js"></script>
6466
<script src="scripts/logger.js"></script>
67+
<script src="scripts/guid.js"></script>
6568
<script src="scripts/session_utils.js"></script>
69+
<script src="scripts/telemetry.js"></script>
6670
<script src="scripts/function_mapping.js"></script>
6771

6872
<script src="scripts/report.js"></script>

demo/v2-demo/scripts/aisdk.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/v2-demo/scripts/assert.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function assert(exp){
2+
if(console["assert"]){
3+
console.assert(exp);
4+
}
5+
}

demo/v2-demo/scripts/guid.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Generates a 20 character uuid.
3+
*/
4+
function generateNewGuid() {
5+
let d = new Date().getTime();
6+
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
7+
d += performance.now();
8+
}
9+
return 'xxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
10+
// Generate a random number, scaled from 0 to 16.
11+
// Bitwise-and by 15 since we only care about the last 4 bits.
12+
const r = (d + Math.random() * 16) & 15 | 0;
13+
14+
// Shift 4 times to divide by 16
15+
d >>= 4;
16+
return r.toString(16);
17+
});
18+
}

demo/v2-demo/scripts/session_utils.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,39 @@ const EntityType = {
2222
Qna: "Qna"
2323
};
2424

25-
var _session = {};
26-
2725
const SessionKeys = {
2826
AccessToken : "accessToken",
27+
DashboardId : "dashboardId",
2928
EmbedUrl : "embedUrl",
3029
EmbedId : "embedId",
31-
DashboardId : "dashboardId",
3230
EmbedMode: "embedMode",
3331
EntityType: "entityType",
3432
GroupId : "groupId",
3533
IsSampleReport: "isSampleReport",
3634
IsSampleDashboard: "IsSampleDashboard",
3735
IsSampleTile: "IsSampleTile",
3836
IsSampleQna: "IsSampleQna",
37+
IsTelemetryEnabled: "isTelemetryEnabled",
3938
PageName: "PageName",
4039
QnaQuestion: "QnaQuestion",
4140
QnaMode: "QnaMode",
4241
SampleId: "SampleId",
42+
SessionId: "SessionId",
4343
TokenType: "tokenType",
4444
VisualName: "VisualName"
4545
};
4646

47+
var _session = {};
48+
49+
function initSession() {
50+
SetSession(SessionKeys.SessionId, generateNewGuid());
51+
if (GetParameterByName(SessionKeys.IsTelemetryEnabled) === "false") {
52+
SetSession(SessionKeys.IsTelemetryEnabled, false);
53+
} else {
54+
SetSession(SessionKeys.IsTelemetryEnabled, true);
55+
}
56+
}
57+
4758
function GetParameterByName(name, url) {
4859
if (!url) {
4960
url = window.location.href;
@@ -317,3 +328,5 @@ function setSession(accessToken, embedUrl, embedId, dashboardId)
317328
SetSession(SessionKeys.EmbedId, embedId);
318329
SetSession(SessionKeys.DashboardId, dashboardId);
319330
}
331+
332+
initSession();

demo/v2-demo/scripts/telemetry.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const TelemetryEventName = {
2+
Homepage : "Homepage",
3+
};
4+
5+
function trackEvent(name, properties, flush = false) {
6+
if (!_session[SessionKeys.IsTelemetryEnabled]) {
7+
return;
8+
}
9+
assert(name && properties);
10+
properties.sessionId = GetSession(SessionKeys.SessionId);
11+
12+
// Normally, the SDK sends data at fixed intervals (typically 30 secs) or whenever buffer is full (typically 500 items).
13+
// https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#flushing-data
14+
appInsights.trackEvent({ name, properties });
15+
if (flush) {
16+
appInsights.flush();
17+
}
18+
}
19+
20+
trackEvent(TelemetryEventName.Homepage, {}, true);

0 commit comments

Comments
 (0)