Skip to content

Commit 020a7f0

Browse files
committed
fix: prevent double firing by setting initialPageView to false
1 parent 8994495 commit 020a7f0

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/load-script.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,7 @@ export default function loadScript(key) {
99

1010
// Create our global event queue to catch any events that
1111
// occur during script load
12-
shimGlobalAnalytics();
13-
14-
// Load in our external analytics script see this page for details:
15-
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement
16-
// Create our script
17-
const script = document.createElement('script');
18-
script.type = 'text/javascript';
19-
// Handle load/error states
20-
script.onload = res;
21-
script.onerror = rej;
22-
// Inject into the head of the document
23-
document.head.prepend(script);
24-
// Assign a src so it **actually** loads
25-
script.src = `https://cdn.segment.com/analytics.js/v1/${key}/analytics.min.js`;
12+
shimGlobalAnalytics(res, rej, key);
2613

2714
// Return the now present window.analytics after the script has loaded
2815
return window.analytics;

src/shim-global-analytics.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function shimGlobalAnalytics() {
1+
export default function shimGlobalAnalytics(res, rej, segmentKey) {
22
// Create a queue, but don't obliterate an existing one!
33
const analytics = window.analytics || [];
44
window.analytics = analytics;
@@ -51,7 +51,34 @@ export default function shimGlobalAnalytics() {
5151
};
5252

5353
// For each of our methods, generate a queueing stub.
54-
analytics.methods.forEach(key => {
55-
analytics[key] = analytics.factory(key);
54+
analytics.methods.forEach(method => {
55+
analytics[method] = analytics.factory(method);
5656
});
57+
58+
// Load in our external analytics script see this page for details:
59+
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement
60+
// Create our script
61+
62+
// Define a method to load Analytics.js from our CDN,
63+
// and that will be sure to only ever load it once.
64+
analytics.load = function(key, options) {
65+
const script = document.createElement('script');
66+
script.type = 'text/javascript';
67+
// Handle load/error states
68+
script.onload = res;
69+
script.onerror = rej;
70+
// Inject into the head of the document
71+
document.head.prepend(script);
72+
// Assign a src so it **actually** loads
73+
script.src = `https://cdn.segment.com/analytics.js/v1/${key}/analytics.min.js`;
74+
75+
analytics._loadOptions = options; // eslint-disable-line
76+
};
77+
78+
// Add a version to keep track of what's in the wild.
79+
analytics.SNIPPET_VERSION = '4.1.0';
80+
81+
// Load Analytics.js with your key, which will automatically
82+
// load the tools you've enabled for your account. Boosh!
83+
analytics.load(segmentKey, { initialPageView: false });
5784
}

0 commit comments

Comments
 (0)