Skip to content

Commit e1fc13b

Browse files
committed
added analytics tracking with segment
1 parent 40878b0 commit e1fc13b

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

app/assets/javascripts/segmentio.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Create a queue, but don't obliterate an existing one!
2+
window.analytics || (window.analytics = []);
3+
4+
// A list of all the methods in analytics.js that we want to stub.
5+
window.analytics.methods = ['identify', 'track', 'trackLink', 'trackForm',
6+
'trackClick', 'trackSubmit', 'page', 'pageview', 'ab', 'alias', 'ready',
7+
'group', 'on', 'once', 'off'];
8+
9+
// Define a factory to create queue stubs. These are placeholders for the
10+
// "real" methods in analytics.js so that you never have to wait for the library
11+
// to load asynchronously to actually track things. The `method` is always the
12+
// first argument, so we know which method to replay the call into.
13+
window.analytics.factory = function (method) {
14+
return function () {
15+
var args = Array.prototype.slice.call(arguments);
16+
args.unshift(method);
17+
window.analytics.push(args);
18+
return window.analytics;
19+
};
20+
};
21+
22+
// For each of our methods, generate a queueing method.
23+
for (var i = 0; i < window.analytics.methods.length; i++) {
24+
var method = window.analytics.methods[i];
25+
window.analytics[method] = window.analytics.factory(method);
26+
}
27+
28+
// Define a method that will asynchronously load analytics.js from our CDN.
29+
window.analytics.load = function (apiKey) {
30+
31+
// Create an async script element for analytics.js based on your API key.
32+
var script = document.createElement('script');
33+
script.type = 'text/javascript';
34+
script.async = true;
35+
script.src = ('https:' === document.location.protocol ? 'https://' : 'http://') +
36+
'd2dq2ahtl5zl1z.cloudfront.net/analytics.js/v1/' + apiKey + '/analytics.min.js';
37+
38+
// Find the first script element on the page and insert our script next to it.
39+
var firstScript = document.getElementsByTagName('script')[0];
40+
firstScript.parentNode.insertBefore(script, firstScript);
41+
};
42+
43+
// Add a version so we can keep track of what's out there in the wild.
44+
window.analytics.SNIPPET_VERSION = '2.0.8';
45+
46+
// Load analytics.js with your API key, which will automatically load all of the
47+
// analytics integrations you've turned on for your account. Boosh!
48+
window.analytics.load('0cbe84vfF9eMWCEWDovxFrbDxy4xShsE');
49+
50+
// Make our first page call to load the integrations. If you'd like to manually
51+
// name or tag the page, edit or move this call to use your own tags.
52+
/* */
53+
window.analytics.page();
54+
55+
// accommodate Turbolinks
56+
// track page views and form submissions
57+
$(document).on('ready page:change', function() {
58+
console.log('page loaded');
59+
analytics.page();
60+
analytics.trackForm($('#new_visitor'), 'Signed Up');
61+
analytics.trackForm($('#new_contact'), 'Contact Request');
62+
})

0 commit comments

Comments
 (0)