-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
98 lines (83 loc) · 3.04 KB
/
index.js
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
98
'use strict';
var merge = require('lodash-node/compat/objects/merge');
var googleAnalyticsConfigDefaults = {
globalVariable: 'ga',
tracker: 'analytics.js',
webPropertyId: null,
cookieDomain: null,
cookieName: null,
cookieExpires: null,
displayFeatures: false
};
function analyticsTrackingCode(config) {
var scriptArray,
displayFeaturesString,
gaConfig = {};
if (config.cookieDomain != null) {
gaConfig.cookieDomain = config.cookieDomain;
}
if (config.cookieName != null) {
gaConfig.cookieName = config.cookieName;
}
if (config.cookieExpires != null) {
gaConfig.cookieExpires = config.cookieExpires;
}
if (Object.keys(gaConfig).length === 0) {
gaConfig = "'auto'";
} else {
gaConfig = JSON.stringify(gaConfig);
}
scriptArray = [
"<script>",
"(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){",
"(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),",
"m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)",
"})(window,document,'script','https://www.google-analytics.com/analytics.js','" + config.globalVariable + "');",
"",
"" + config.globalVariable + "('create', '" + config.webPropertyId + "', " + gaConfig + ");",
"</script>"
];
if (config.displayFeatures) {
displayFeaturesString = "" + config.globalVariable + "('require', 'displayfeatures');";
scriptArray.splice(-2, 0, displayFeaturesString);
}
return scriptArray;
}
function gaTrackingCode(config) {
var scriptArray;
scriptArray = [
"<script>",
"var _gaq = _gaq || [];",
"_gaq.push(['_setAccount', '" + config.webPropertyId + "']);",
"_gaq.push(['_trackPageview']);",
"",
"(function() {",
" 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);",
"})();",
"</script>"
];
if (config.displayFeatures) {
scriptArray.splice(-4, 1, " ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';");
}
return scriptArray;
}
module.exports = {
name: 'ember-cli-google-analytics',
contentFor: function(type, config) {
var googleAnalyticsConfig = merge({}, googleAnalyticsConfigDefaults, config.googleAnalytics || {});
if (type === 'head' && googleAnalyticsConfig.webPropertyId != null) {
var content;
if (googleAnalyticsConfig.tracker === 'analytics.js') {
content = analyticsTrackingCode(googleAnalyticsConfig);
} else if (googleAnalyticsConfig.tracker === 'ga.js') {
content = gaTrackingCode(googleAnalyticsConfig);
} else {
throw new Error('Invalid tracker found in configuration: "' + googleAnalyticsConfig.tracker + '". Must be one of: "analytics.js", "ga.js"');
}
return content.join("\n");
}
return '';
}
};