cloudflare/InstantPlugin
Publicmirrored fromhttps://github.com/cloudflare/InstantPlugin
app/segment.js
88lines · modecode
unknown
| 1 | (function initSegment() { |
| 2 | // Create a queue, but don't obliterate an existing one! |
| 3 | const analytics = window.analytics = window.analytics || [] |
| 4 | |
| 5 | // If the real analytics.js is already on the page return. |
| 6 | if (analytics.initialize) return |
| 7 | |
| 8 | // If the snippet was invoked already show an error. |
| 9 | if (analytics.invoked) { |
| 10 | if (window.console && console.error) { |
| 11 | console.error("Segment snippet included twice.") |
| 12 | } |
| 13 | return |
| 14 | } |
| 15 | |
| 16 | // Invoked flag, to make sure the snippet |
| 17 | // is never invoked twice. |
| 18 | analytics.invoked = true |
| 19 | |
| 20 | // A list of the methods in Analytics.js to stub. |
| 21 | analytics.methods = [ |
| 22 | "trackSubmit", |
| 23 | "trackClick", |
| 24 | "trackLink", |
| 25 | "trackForm", |
| 26 | "pageview", |
| 27 | "identify", |
| 28 | "reset", |
| 29 | "group", |
| 30 | "track", |
| 31 | "ready", |
| 32 | "alias", |
| 33 | "page", |
| 34 | "once", |
| 35 | "off", |
| 36 | "on" |
| 37 | ] |
| 38 | |
| 39 | // Define a factory to create stubs. These are placeholders |
| 40 | // for methods in Analytics.js so that you never have to wait |
| 41 | // for it to load to actually record data. The `method` is |
| 42 | // stored as the first argument, so we can replay the data. |
| 43 | analytics.factory = function(method) { |
| 44 | return function() { |
| 45 | const args = Array.prototype.slice.call(arguments) |
| 46 | |
| 47 | args.unshift(method) |
| 48 | analytics.push(args) |
| 49 | return analytics |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // For each of our methods, generate a queueing stub. |
| 54 | for (let i = 0; i < analytics.methods.length; i++) { |
| 55 | const key = analytics.methods[i] |
| 56 | |
| 57 | analytics[key] = analytics.factory(key) |
| 58 | } |
| 59 | |
| 60 | // Define a method to load Analytics.js from our CDN, |
| 61 | // and that will be sure to only ever load it once. |
| 62 | analytics.load = key => { |
| 63 | // Create an async script element based on your key. |
| 64 | const script = document.createElement("script") |
| 65 | |
| 66 | script.type = "text/javascript" |
| 67 | script.async = true |
| 68 | script.src = (document.location.protocol === "https:" ? "https://" : "http://") + |
| 69 | `cdn.segment.com/analytics.js/v1/${key}/analytics.min.js` |
| 70 | |
| 71 | // Insert our script next to the first script element. |
| 72 | const first = document.getElementsByTagName("script")[0] |
| 73 | |
| 74 | first.parentNode.insertBefore(script, first) |
| 75 | } |
| 76 | |
| 77 | // Add a version to keep track of what"s in the wild. |
| 78 | analytics.SNIPPET_VERSION = "3.1.0" |
| 79 | |
| 80 | // Load Analytics.js with your key, which will automatically |
| 81 | // load the tools you"ve enabled for your account. Boosh! |
| 82 | analytics.load("UeTgchn085EGxJh93fJWBSoUQQsY1Vr9") |
| 83 | |
| 84 | // Make the first page call to load the integrations. If |
| 85 | // you"d like to manually name or tag the page, edit or |
| 86 | // move this call however you"d like. |
| 87 | analytics.page() |
| 88 | }()) |