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