Prevent duplicate data if snippet is inserted twice

This commit is contained in:
Kamil Szostak 2017-06-07 17:50:32 -07:00
Родитель 787b918fc7
Коммит c45320aeff
3 изменённых файлов: 28 добавлений и 4 удалений

Просмотреть файл

@ -35,7 +35,7 @@ $content | out-file "$($projectDir)\E2ETests\ai.js"
$aiPath = "ai.js"
# test the queue
$queueTest = "var i = 100; while(i--){appInsights.queue.push(function() {window.queueTest('from the queue')})};"
$queueTest = "var i = 100; while(i--){appInsights.queue && appInsights.queue.push(function() {window.queueTest('from the queue')})};"
# copy snippet and convert protocol to file://
$snippetLatest = gc "$($projectDir)\..\JavaScriptSDK\snippet.js"

Просмотреть файл

@ -13,9 +13,11 @@ class SnippetTests extends TestClass {
private queueCallCount = 100;
private senderMocks;
private loadSnippet(path) {
private loadSnippet(path, resetWindow = true) {
// load ai via the snippet
window[this.aiName] = undefined;
if (resetWindow) {
window[this.aiName] = undefined;
}
var key = "E2ETests";
var snippetPath = window.location.href.split(key)[0] + key + path;
var scriptElement = document.createElement("script");
@ -144,6 +146,24 @@ class SnippetTests extends TestClass {
Assert.equal("upps!", (<any>data.data).baseData.exceptions[0].message, "error has correct message");
})
});
var trackPageSpy: SinonSpy;
this.testCaseAsync({
name: "SnippetTests: it's safe to initialize the snippet twice, but it should report only one pageView",
stepDelay: 250,
steps: [
() => {
this.loadSnippet(snippet_Latest);
},
() => {
trackPageSpy = this.sandbox.spy(window["appInsights"], "trackPageView");
this.loadSnippet(snippet_Latest, false);
},
() => {
Assert.equal(trackPageSpy.callCount, 0);
}]
});
}
private testSnippet(snippetPath) {

Просмотреть файл

@ -78,4 +78,8 @@
// global instance must be set in this order to mitigate issues in ie8 and lower
window.appInsights = appInsights;
appInsights.trackPageView();
// if somebody calls the snippet twice, don't report page view again
if (appInsights.queue && appInsights.queue.length === 0) {
appInsights.trackPageView();
}