Skip to content

Commit

Permalink
Update the snippet that loads Segment
Browse files Browse the repository at this point in the history
I took it from more recent Segment documentation. This small snippet is
versioned. These changes upgrade it from 2.0.9 to 4.1.0
  • Loading branch information
dcyriller committed Apr 29, 2020
1 parent 755707c commit 77c0712
Showing 1 changed file with 87 additions and 45 deletions.
132 changes: 87 additions & 45 deletions addon/metrics-adapters/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,94 @@ export default BaseAdapter.extend({

assert(`[ember-metrics] You must pass a valid \`key\` to the ${this.toString()} adapter`, segmentKey);

/* eslint-disable */
(window.analytics = window.analytics || []),
(window.analytics.methods = [
"identify",
"group",
"track",
"page",
"pageview",
"alias",
"ready",
"on",
"once",
"off",
"trackLink",
"trackForm",
"trackClick",
"trackSubmit",
]),
(window.analytics.factory = function (t) {
return function () {
var a = Array.prototype.slice.call(arguments);
return a.unshift(t), window.analytics.push(a), window.analytics;
};
});
for (var i = 0; i < window.analytics.methods.length; i++) {
var key = window.analytics.methods[i];
window.analytics[key] = window.analytics.factory(key);
}
(window.analytics.load = function (t) {
if (!document.getElementById("analytics-js")) {
var a = document.createElement("script");
(a.type = "text/javascript"),
(a.id = "analytics-js"),
(a.async = !0),
(a.src =
("https:" === document.location.protocol ? "https://" : "http://") +
"cdn.segment.com/analytics.js/v1/" +
t +
"/analytics.min.js");
var n = document.getElementsByTagName("script")[0];
n.parentNode.insertBefore(a, n);
// start of segment loading snippet, taken here:
// https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/quickstart/#step-1-copy-the-snippet

/* eslint-disable no-console */

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

// If the real analytics.js is already on the page return.
if (analytics.initialize) return;

// If the snippet was invoked already show an error
if (analytics.invoked) {
if (window.console && console.error) {
console.error('Segment snippet included twice.');
}
}),
(window.analytics.SNIPPET_VERSION = "2.0.9");
/* eslint-enable */
window.analytics.load(segmentKey);
return;
}

// Invoked flag, to make sure the snippet
// is never invoked twice.
analytics.invoked = true;

// A list of the methods in Analytics.js to stub.
analytics.methods = [
'trackSubmit',
'trackClick',
'trackLink',
'trackForm',
'pageview',
'identify',
'reset',
'group',
'track',
'ready',
'alias',
'debug',
'page',
'once',
'off',
'on'
];

// Define a factory to create stubs. These are placeholders
// for methods in Analytics.js so that you never have to wait
// for it to load to actually record data. The `method` is
// stored as the first argument, so we can replay the data.
analytics.factory = function(method){
return function(){
var args = Array.prototype.slice.call(arguments);
args.unshift(method);
analytics.push(args);
return analytics;
};
};

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

// Define a method to load Analytics.js from our CDN,
// and that will be sure to only ever load it once.
analytics.load = function(key, options){
// Create an async script element based on your key.
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = 'https://cdn.segment.com/analytics.js/v1/'
+ key + '/analytics.min.js';

// Insert our script next to the first script element.
var first = document.getElementsByTagName('script')[0];
first.parentNode.insertBefore(script, first);
analytics._loadOptions = options;
};

// Add a version to keep track of what's in the wild.
analytics.SNIPPET_VERSION = '4.1.0';

// Load Analytics.js with your key, which will automatically
// load the tools you've enabled for your account.
analytics.load(segmentKey);

/* eslint-enable no-console */

// end of segment loading snippet
},

alias(options = {}) {
Expand Down

0 comments on commit 77c0712

Please sign in to comment.