From 755707c3e071481e5ff12d8c672a6192e712f213 Mon Sep 17 00:00:00 2001 From: Cyrille David Date: Wed, 29 Apr 2020 11:54:32 +0200 Subject: [PATCH 1/2] Prettify Segment snippet I ran `prettier --write segment-snippet` --- addon/metrics-adapters/segment.js | 46 +++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/addon/metrics-adapters/segment.js b/addon/metrics-adapters/segment.js index 4bf6c927..6933eb41 100644 --- a/addon/metrics-adapters/segment.js +++ b/addon/metrics-adapters/segment.js @@ -17,7 +17,49 @@ 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 Date: Thu, 7 Jun 2018 15:16:18 +0200 Subject: [PATCH 2/2] Update the snippet that loads Segment 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 --- addon/metrics-adapters/segment.js | 132 ++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 45 deletions(-) diff --git a/addon/metrics-adapters/segment.js b/addon/metrics-adapters/segment.js index 6933eb41..e803ed15 100644 --- a/addon/metrics-adapters/segment.js +++ b/addon/metrics-adapters/segment.js @@ -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 = {}) {