Skip to content

Commit

Permalink
Merge pull request adopted-ember-addons#240 from dcyriller/segment-up…
Browse files Browse the repository at this point in the history
…grade

Update Segment's loading snippet
  • Loading branch information
dcyriller authored May 1, 2020
2 parents 785cb10 + 77c0712 commit 06b3a87
Showing 1 changed file with 89 additions and 5 deletions.
94 changes: 89 additions & 5 deletions addon/metrics-adapters/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +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)}},window.analytics.SNIPPET_VERSION="2.0.9";
/* eslint-enable */
window.analytics.load(segmentKey);
// 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.');
}
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 Expand Up @@ -61,5 +145,5 @@ export default BaseAdapter.extend({
removeFromDOM('script[src*="segment.com"]');

delete window.analytics;
}
},
});

0 comments on commit 06b3a87

Please sign in to comment.