Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pin variable name for global key #143

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var lineRegex = /(<%= settings\.load %>|<%= settings\.page %>|}}\(\);)/g;
var versionRegex = /<%= settings\.version %>/;
var snippet = template(source.replace(optionalCDNRegex, '$1').replace(loadRegex, '$1').replace(pageRegex, '$1').replace(versionRegex, packageJSON.version), { variable: 'settings' });
var snippetMin = template(minify(source, {
mangle: { except: ['analytics', 'key', 't', 'e', 'p', 'c'] },
mangle: { except: ['analytics', 'key', 't', 'e', 'p', 'c', 'i'], },
compress: { sequences: false, side_effects: false, },
fromString: true
}).code.replace(optionalCDNRegex, '$1').replace(loadRegex, '$1').replace(pageRegex, '$1').replace(lineRegex, '\n$1').replace(versionRegex, packageJSON.version), { variable: 'settings' });
Expand Down
14 changes: 7 additions & 7 deletions template/snippet.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(function() {
// define the key where the global analytics object will be accessible
// customers can safely set this to be something else if need be
var globalAnalyticsKey = "<%= settings.globalAnalyticsKey %>"
var i = "<%= settings.globalAnalyticsKey %>"

// Create a queue, but don't obliterate an existing one!
var analytics = window[globalAnalyticsKey] = window[globalAnalyticsKey] || [];
var analytics = window[i] = window[i] || [];

// If the real analytics.js is already on the page return.
if (analytics.initialize) return;
Expand Down Expand Up @@ -53,10 +53,10 @@
// stored as the first argument, so we can replay the data.
analytics.factory = function(e) {
return function() {
if (window[globalAnalyticsKey].initialized) {
if (window[i].initialized) {
// Sometimes users assigned analytics to a variable before analytics is done loading, resulting in a stale reference.
// If so, proxy any calls to the 'real' analytics instance.
return window[globalAnalyticsKey][e].apply(window[globalAnalyticsKey], arguments);
return window[i][e].apply(window[i], arguments);
}
var args = Array.prototype.slice.call(arguments);

Expand All @@ -82,8 +82,8 @@


// For each of our methods, generate a queueing stub.
for (var i = 0; i < analytics.methods.length; i++) {
var key = analytics.methods[i];
for (var j = 0; j < analytics.methods.length; j++) {
var key = analytics.methods[j];
analytics[key] = analytics.factory(key);
}

Expand All @@ -94,7 +94,7 @@
var t = document.createElement("script");
t.type = "text/javascript";
t.async = true;
t.setAttribute("data-global-segment-analytics-key", globalAnalyticsKey)
t.setAttribute("data-global-segment-analytics-key", i)
t.src = "https://<%= settings.host %><%= settings.ajsPath %>";

// Insert our script next to the first script element.
Expand Down
Loading