forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.google-analytics.js
More file actions
97 lines (86 loc) · 3.29 KB
/
Copy pathjquery.google-analytics.js
File metadata and controls
97 lines (86 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
define([
'INST' /* INST */,
'jquery' /* $ */
], function(INST, $) {
// requires INST global
window._gaq = window._gaq || [];
var asyncScriptInserted = false;
/**
* Enables Google Analytics tracking on the page from which it's called.
*
* Usage:
* $.trackPage('UA-xxx-xxx', options);
*
* Parameters:
* account_id - Your Google Analytics account ID.
* options - An object containing one or more optional parameters:
* - status_code - The HTTP status code of the current server response.
* If this is set to something other than 200 then the page is tracked
* as an error page. For more details: http://antezeta.com/news/404-errors-google-analytics
*
*/
$.trackPage = function(account_id, options) {
if (!asyncScriptInserted) {
asyncScriptInserted = true;
// insert ga.js async
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
}
options = $.extend({status_code: 200}, options);
window._gaq.push(['_setAccount', account_id]);
if (options.domain) {
window._gaq.push(['_setDomainName', options.domain]);
}
window._gaq.push(['_trackPageview']);
window._gaq.push(['_trackPageLoadTime']);
if (options.status_code != 200) {
window._gaq.push(['_trackEvent', 'Errors', options.status_code, 'page: ' + document.location.pathname + document.location.search + ' ref: ' + document.referrer, options.error_id ]);
}
};
// see: http://code.google.com//apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._setCustomVar
$.setTrackingVar = function() {
var args = Array.prototype.slice.call( arguments, 0 );
args.unshift('_setCustomVar');
window._gaq.push.apply(window._gaq, args);
};
/**
* Tracks an event using the given parameters.
*
* The trackEvent method takes four arguments:
*
* category - required string used to group events
* action - required string used to define event type, eg. click, download
* label - optional label to attach to event, eg. buy
* value - optional numerical value to attach to event, eg. price
*
* see: http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
*/
$.trackEvent = function(category, action, label, value) {
window._gaq.push(['_trackEvent', category, action, label, value]);
};
/**
* simultates tracking a page view. Usage:
*
* $.trackPageView("/path/to/url/to/track")
*
* see: http://code.google.com//apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._trackPageview
*/
$.trackPageview = function(url) {
window._gaq.push(['_trackPageview', url]);
};
// this next part is the only part that is Instructure specific
if (INST && INST.googleAnalyticsAccount) {
$.trackPage(INST.googleAnalyticsAccount, {
status_code: INST.http_status,
error_id: INST.error_id,
domainName: document.location.hostname
});
}
return {
trackPage: $.trackPage,
setTrackingVar: $.setTrackingVar,
trackEvent: $.trackEvent,
trackPageView: $.trackPageView
};
});