├── HISTORY.md ├── LICENSE ├── README.md ├── index.js └── package.json /HISTORY.md: -------------------------------------------------------------------------------- 1 | # 2.1.2 (2016-01-31) 2 | 3 | * fix loader to compensate for segment queue object 4 | 5 | # 2.1.1 (2016-01-01) 6 | 7 | * use https: protocol by default 8 | - segment script is loaded from a secure request if current protocol is not http: (for chrome-extension: for example) 9 | 10 | # 2.1.0 (2015-10-27) 11 | 12 | * add reset method 13 | 14 | # 2.0.0 (2015-10-06) 15 | 16 | * fix loader to not pollute global scope 17 | - the function returns the analytics object now instead of messing with globals 18 | 19 | # 1.1.0 (2015-04-24) 20 | 21 | * add skipPageCall param 22 | 23 | # 1.0.1 (2015-02-10) 24 | 25 | * fix snippet integration, do no try to be smart 26 | 27 | # 1.0.0 (2015-01-29) 28 | 29 | * initial 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Vincent Voyer 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # analytics.js-loader 2 | 3 | This is the [segment.com snippet](https://segment.com/docs/libraries/analytics.js/quickstart/#step-1-copy-the-snippet) *Slightly* modified to be a commonJS compatible module. 4 | 5 | It's usable with module loaders like [browserify](http://browserify.org/) or [webpack](http://webpack.github.io/docs/). 6 | 7 | ## Install 8 | 9 | ```shell 10 | npm install analytics.js-loader --save 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | var analytics = require('analytics.js-loader')({ 17 | writeKey: 'YOUR_WRITE_KEY', 18 | // you can skip the first analytics.page() call if needed, #1 19 | skipPageCall: false 20 | }); 21 | 22 | analytics.identify('1e810c197e', { 23 | name: 'Bill Lumbergh', 24 | email: 'bill@initech.com' 25 | }); 26 | 27 | analytics.track('Signed Up', { 28 | plan: 'Startup', 29 | source: 'Analytics Academy' 30 | }); 31 | ``` 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = load; 2 | 3 | var analytics = []; 4 | 5 | function load(opts) { 6 | // If the real analytics.js is already on the page return. 7 | if (analytics.initialize) return; 8 | 9 | // If the snippet was invoked already show an error. 10 | if (analytics.invoked) { 11 | if (window.console && console.error) { 12 | console.error('Segment snippet included twice.'); 13 | } 14 | return; 15 | } 16 | 17 | // Invoked flag, to make sure the snippet 18 | // is never invoked twice. 19 | analytics.invoked = true; 20 | 21 | // A list of the methods in Analytics.js to stub. 22 | analytics.methods = [ 23 | 'trackSubmit', 24 | 'trackClick', 25 | 'trackLink', 26 | 'trackForm', 27 | 'pageview', 28 | 'identify', 29 | 'group', 30 | 'track', 31 | 'ready', 32 | 'reset', 33 | 'alias', 34 | 'page', 35 | 'once', 36 | 'off', 37 | 'on' 38 | ]; 39 | 40 | // Define a factory to create stubs. These are placeholders 41 | // for methods in Analytics.js so that you never have to wait 42 | // for it to load to actually record data. The `method` is 43 | // stored as the first argument, so we can replay the data. 44 | analytics.factory = function(method){ 45 | return function(){ 46 | var args = Array.prototype.slice.call(arguments); 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 (var i = 0; i < analytics.methods.length; i++) { 55 | var key = analytics.methods[i]; 56 | analytics[key] = analytics.factory(key); 57 | } 58 | 59 | // Define a method to load Analytics.js from our CDN, 60 | // and that will be sure to only ever load it once. 61 | analytics.load = function(key){ 62 | // Create an async script element based on your key. 63 | var script = document.createElement('script'); 64 | script.type = 'text/javascript'; 65 | script.async = true; 66 | script.src = ('http:' === document.location.protocol 67 | ? 'http://' : 'https://') 68 | + 'cdn.segment.com/analytics.js/v1/' 69 | + key + '/analytics.min.js'; 70 | 71 | // Insert our script next to the first script element. 72 | var first = document.getElementsByTagName('script')[0]; 73 | first.parentNode.insertBefore(script, first); 74 | }; 75 | 76 | // Add a version to keep track of what's in the wild. 77 | analytics.SNIPPET_VERSION = '3.0.1'; 78 | 79 | // Load Analytics.js with your key, which will automatically 80 | // load the tools you've enabled for your account. Boosh! 81 | analytics.load(opts.writeKey); 82 | 83 | // Make the first page call to load the integrations. If 84 | // you'd like to manually name or tag the page, edit or 85 | // move this call however you'd like. 86 | if (!opts.skipPageCall) { 87 | analytics.page(); 88 | } 89 | 90 | /* 91 | * The analytics.js-loader module returns the "initial" analytics 92 | * object, which is basically just a queue to hold events until the 93 | * "real" one is loaded with a global script. Problem is, the "real" 94 | * one simply replaces `window.analytics`, and doesn't change over 95 | * this initial "queue" object, meaning that nothing gets registered 96 | * after the "real" one is loaded - things just keep adding to the queue. 97 | * 98 | * This is why we save off `initialAnalytics`, and then always run 99 | * methods through `window.analytics`, which might be the queue, and 100 | * might be the real one. 101 | */ 102 | 103 | var initialAnalytics = analytics; 104 | var actualAnalytics = {}; 105 | 106 | window.analytics = initialAnalytics; 107 | 108 | initialAnalytics.methods.forEach(function(method) { 109 | actualAnalytics[method] = function() { 110 | window.analytics[method].apply(window.analytics, arguments); 111 | } 112 | }); 113 | 114 | return actualAnalytics; 115 | } 116 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "analytics.js-loader", 3 | "version": "2.1.2", 4 | "description": "Asynchronously load segment.com analytics.js with an npm module", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/vvo/analytics.js-loader.git" 12 | }, 13 | "keywords": [ 14 | "analytics.js", 15 | "segment.com", 16 | "loader", 17 | "browserify" 18 | ], 19 | "author": "Vincent Voyer ", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/vvo/analytics.js-loader/issues" 23 | }, 24 | "homepage": "https://github.com/vvo/analytics.js-loader" 25 | } 26 | --------------------------------------------------------------------------------