Skip to content

Commit b007233

Browse files
committed
Merge remote-tracking branch 'powerbi/master'
2 parents aa17ba9 + 08156db commit b007233

File tree

15 files changed

+136
-18
lines changed

15 files changed

+136
-18
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/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function OpenEmbedWorkspace(activeTabSelector, samplesStepHtml)
2727

2828
$(".content").hide();
2929
$("#sampleContent").show();
30-
OpenSamplesStep();
3130

3231
LayoutShowcaseState.layoutReport = null;
3332
BookmarkShowcaseState.bookmarksReport = null;
@@ -44,6 +43,7 @@ function OpenDocumentationSection() {
4443

4544
$(".content").hide();
4645
$("#documentationContent").show();
46+
trackEvent(TelemetryEventName.SectionOpen, { section: TelemetrySectionName.Documentation, src: TelemetryEventSource.UserClick });
4747
}
4848

4949
function OpenShowcasesSection() {
@@ -58,6 +58,7 @@ function OpenShowcasesSection() {
5858

5959
$(".content").hide();
6060
$("#showcasesContent").show();
61+
trackEvent(TelemetryEventName.SectionOpen, { section: TelemetrySectionName.Showcase, src: TelemetryEventSource.UserClick });
6162
}
6263

6364
function SetActiveStyle(id)
@@ -77,6 +78,7 @@ const ShowcasesHtmls = {
7778
function OpenShowcase(showcaseType) {
7879
$("#showcasesContent").load(ShowcasesHtmls[showcaseType]);
7980
showcasesContentLoaded = false;
81+
trackEvent(TelemetrySectionName.Showcase, { showcaseType: showcaseType, src: TelemetryEventSource.UserClick });
8082
}
8183

8284
function OpenShowcaseFromURL(showcase) {
@@ -85,4 +87,5 @@ function OpenShowcaseFromURL(showcase) {
8587

8688
$(".content").hide();
8789
$("#showcasesContent").show();
90+
trackEvent(TelemetrySectionName.Showcase, { showcaseType: showcase, src: TelemetryEventSource.Url });
8891
}

demo/v2-demo/scripts/report.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ function OpenSamplesStep() {
3030
$("#embed-and-interact-steps-wrapper").hide();
3131

3232
$("#welcome-text").show();
33+
34+
trackEvent(TelemetryEventName.InnerSectionOpen, { section: TelemetryInnerSection.Sample, src: TelemetryEventSource.UserClick });
3335
}
3436

3537
function OpenCodeStepFromNavPane()
@@ -79,11 +81,20 @@ function OpenCodeStep(mode, entityType, tokenType) {
7981
$('#interact-tab').removeClass(active_tabs_li);
8082

8183
LoadEmbedSettings(mode, entityType, tokenType);
84+
85+
trackEvent(TelemetryEventName.InnerSectionOpen, { section: TelemetryInnerSection.Code, src: TelemetryEventSource.UserClick });
8286
}
8387

8488
function bootstrapIframe(entityType) {
8589
const activeContainer = getActiveEmbedContainer();
8690

91+
// To avoid multiple bootstrap when switching between Desktop view and Phone view
92+
// and also when changing the mode (view/edit/create).
93+
if (activeContainer.children.length > 0) {
94+
// entity is already embedded
95+
return;
96+
}
97+
8798
// Bootstrap iframe - for better performance.
8899
let embedUrl = GetSession(SessionKeys.EmbedUrl);
89100
config = {
@@ -574,6 +585,7 @@ function EmbedAreaDesktopView() {
574585
runFunc();
575586
}
576587
}
588+
trackEvent(TelemetryEventName.DesktopModeOpen, {});
577589
}
578590

579591
function EmbedAreaMobileView() {
@@ -631,6 +643,7 @@ function EmbedAreaMobileView() {
631643
}
632644
}
633645
}
646+
trackEvent(TelemetryEventName.MobileModeOpen, {});
634647
}
635648

636649
function updateRunFuncSessionParameters(runFunc) {
@@ -684,5 +697,6 @@ function hideFeaturesOnMobile(){
684697

685698
function onShowcaseTryMeClicked(showcase) {
686699
let showcaseUrl = location.href.substring(0, location.href.lastIndexOf("/")) + '?showcase=' + showcase;
700+
trackEvent(TelemetrySectionName.Showcase, { showcaseType: showcase, src: TelemetryEventSource.Interact });
687701
window.open(showcaseUrl, '_blank');
688702
}

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/step_samples.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ function OpenCodeStepWithSample(entityType) {
3232
SetSession(SessionKeys.IsSampleQna, true);
3333
OpenCodeStep(EmbedViewMode, EntityType.Qna)
3434
}
35+
else {
36+
assert(false);
37+
trackEvent(TelemetryEventName.CodeStepError, {});
38+
return;
39+
}
40+
trackEvent(TelemetrySectionName.SampleTool, { entityType: entityType, src: TelemetryEventSource.UserClick });
3541
}

demo/v2-demo/scripts/telemetry.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const TelemetryEventName = {
2+
CodeStepError: "CodeStepError",
3+
CopyCode: "CopyCode",
4+
CopyLog: "CopyLog",
5+
DesktopModeOpen: "DesktopModeOpen",
6+
InnerSectionOpen: "InnerSectionOpen",
7+
Interact: "Interact",
8+
MobileModeOpen: "MobileModeOpen",
9+
RunClick: "RunClick",
10+
SectionOpen: "SectionOpen",
11+
SessionStart: "SessionStart"
12+
};
13+
const TelemetryEventSource = {
14+
Url: "Url",
15+
UserClick: "UserClick"
16+
};
17+
18+
const TelemetryInnerSection = {
19+
Code: "Code",
20+
Sample: "Sample"
21+
};
22+
23+
const TelemetrySectionName = {
24+
Documentation: "Documentation",
25+
SampleTool: "SampleTool",
26+
Showcase: "Showcase"
27+
};
28+
29+
function trackEvent(name, properties, flush = false) {
30+
if (!_session[SessionKeys.IsTelemetryEnabled]) {
31+
return;
32+
}
33+
assert(name && properties);
34+
properties.sessionId = GetSession(SessionKeys.SessionId);
35+
36+
// Normally, the SDK sends data at fixed intervals (typically 30 secs) or whenever buffer is full (typically 500 items).
37+
// https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#flushing-data
38+
appInsights.trackEvent({ name, properties });
39+
if (flush) {
40+
appInsights.flush();
41+
}
42+
}
43+
44+
trackEvent(TelemetryEventName.SessionStart, { referrer: document.referrer }, true);

demo/v2-demo/scripts/utils.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ function SetCode(func) {
5454

5555
if (func != "") {
5656
let runFunc = mapFunc(func);
57-
58-
if (getFuncName(runFunc).match(/Embed/)) {
57+
let funcName = getFuncName(runFunc);
58+
if (funcName.match(/Embed/)) {
5959
let oldFunc = runFunc;
6060
runFunc = function() {
6161
oldFunc();
@@ -74,6 +74,7 @@ function SetCode(func) {
7474
showEmbedContainer();
7575
removeIframeIfUrlIsChanged();
7676
elementClicked('#btnRunCode');
77+
trackEvent(TelemetryEventName.RunClick, { EmbedType: GetSession(SessionKeys.EntityType), TokenType: GetSession(SessionKeys.TokenType), ApiUsed: funcName });
7778
runFunc();
7879
});
7980
// TODO: add indication to click Interact tab on first embedding
@@ -92,10 +93,12 @@ function CopyCode() {
9293

9394
textarea.value = currentCode;
9495
CopyTextArea('#' + id, "#btnRunCopyCode");
96+
trackEvent(TelemetryEventName.CopyCode, {});
9597
}
9698

9799
function CopyResponseWindow() {
98100
CopyTextArea("#txtResponse", "#btnCopyResponse");
101+
trackEvent(TelemetryEventName.CopyLog, {});
99102
}
100103

101104
function CopyTextArea(textAreaSelector, buttonSelector) {
@@ -179,7 +182,9 @@ function removeIframeIfUrlIsChanged() {
179182
return;
180183
}
181184

182-
let existingIframeUrl = removeUidFromUrl(activeContainer.powerBiEmbed.iframe.src);
185+
let existingIframeUrl = removeArgFromUrl(activeContainer.powerBiEmbed.iframe.src, "uid");
186+
existingIframeUrl = removeArgFromUrl(existingIframeUrl, "isMobile");
187+
183188
let embedUrl = GetSession(SessionKeys.EmbedUrl);
184189

185190
if (embedUrl !== existingIframeUrl) {
@@ -216,12 +221,12 @@ function SetAuthoringPageActive(report) {
216221
});
217222
}
218223

219-
function removeUidFromUrl(url) {
220-
const uidRegEx = /uid="?([^&]+)"?/
221-
const uidMatch = url.match(uidRegEx);
224+
function removeArgFromUrl(url, arg) {
225+
const argRegEx = new RegExp(arg + '="?([^&]+)"?')
226+
const argMatch = url.match(argRegEx);
222227

223-
if (uidMatch) {
224-
return url.replace("&" + uidMatch[0], "");
228+
if (argMatch) {
229+
return url.replace("&" + argMatch[0], "");
225230
}
226231

227232
return url;

0 commit comments

Comments
 (0)