forked from SDITools/Airlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airlock.js
280 lines (243 loc) · 8.61 KB
/
airlock.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* ---------------------------------------------------------------------------
d8888 8888888 8888888b. 888 .d88888b. .d8888b. 888 d8P
d88888 888 888 Y88b 888 d88P" "Y88b d88P Y88b 888 d8P
d88P888 888 888 888 888 888 888 888 888 888 d8P
d88P 888 888 888 d88P 888 888 888 888 888d88K
d88P 888 888 8888888P" 888 888 888 888 8888888b
d88P 888 888 888 T88b 888 888 888 888 888 888 Y88b
d8888888888 888 888 T88b 888 Y88b. .d88P Y88b d88P 888 Y88b
d88P 888 8888888 888 T88b 88888888 "Y88888P" "Y8888P" 888 Y88b
------------------------------------------------------------------------------
Airlock v0.1.0
(c) 2013 by Search Discovery <http://searchdiscovery.com/>
airlock.js may be freely distributed under the MIT license.
For all details and documentation: http://www.searchdiscovery.com/airlock
----------------------------------------------------------------------------*/
(function (window, document, undefined) {
// First thing's first; load the new hotness.
(function(a, i, r, l, o, c, k) {
a.GoogleAnalyticsObject = o;
a[o] = a[o] || (function() {
(a[o].q = a[o].q || []).push(arguments);
});
a[o].l = 1 * (new Date());
c = i.createElement(r);
k = i.getElementsByTagName(r)[0];
c.async = 1;
c.src = l;
k.parentNode.insertBefore(c, k);
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
var _gaq = window._gaq,
rx = {
actions: /^([\w\d_-]+)?\.?(_track(Event|Pageview|Trans|Social|Timing)|_add(Item|Trans)|_set(CustomVar|Account|DomainName|AllowLinker|SampleRate|CookiePath)?|_link|_require)$/,
setupActions: /^(.+\.)?_(set(Account|CustomVar|DomainName|AllowLinker|SampleRate|CookiePath)?)$/,
ecommerceActions: /^(.+\.)?_(add(Trans|Item)|trackTrans)$/,
writeableSet: /^page|title$/
}, i, ln;
var Store = function () {
this._contents = {};
this._defaultKey = '__default__';
};
Store.prototype.get = function (key) {
key = key || this._defaultKey;
return this._contents[key];
};
Store.prototype.set = function (key, val) {
key = key || this._defaultKey;
this._contents[key] = val;
return this;
};
Store.prototype.each = function (func, context) {
for (var key in this._contents) {
func.call(context, this._contents[key], key, this._contents);
}
};
// Each spaceship represents a tracker namespace.
var SpaceShip = function (options) {
this.settings = {};
this._settings = {};
this.account = options.account;
this.namespace = options.namespace;
this.setupQueue = [];
this.settings.name = this.namespace;
};
var Airlock = {};
Airlock.spaceships = new Store();
Airlock.readAction = function (actionName) {
var match = actionName.match(rx.actions);
return {
namespace: match[1],
action: match[2]
};
};
Airlock.initialize = function () {
var newQ = [], action, spaceship;
// loop through _gaq to strip out setup calls
for (i = 0, ln = _gaq.length; i < ln; i++) {
spaceship = null;
action = Airlock.readAction(_gaq[i][0]);
if (/_setAccount/.test(action.action)) {
spaceship = new SpaceShip({
namespace: action.namespace,
account: _gaq[i][1]
});
this.dock(spaceship);
continue;
}
if (rx.setupActions.test(action.action)) {
this.pressurize(this.spaceships.get(action.namespace), _gaq[i]);
continue;
}
newQ.push(_gaq[i]);
}
Array.prototype.splice.apply(window._gaq, [0, _gaq.length].concat(newQ));
this.spaceships.each(function (spaceship) {
Airlock.open(spaceship, [
'create',
spaceship.account,
spaceship.settings
]);
});
// No need to do anything fancy here. Setting an instance method
// `push()` on the global `_gaq` variable will intercept any calls
// to `_gaq.prototype.push()`
window._gaq.push = function (args) {
var action = args[0];
// Ensure users are sending a valid action, otherwise do nothing.
if (!rx.actions.test(action)) { return; }
// If the user is trying to setup/send an ecommerce action
if (rx.ecommerceActions.test(action) && !Airlock.ecommerceInitialized) {
Airlock.ecommerceInitialized = true;
window.ga('require', 'ecommerce', 'ecommerce.js');
}
var spaceship = Airlock.spaceships.get(Airlock.readAction(action).namespace);
if (!spaceship) { return; }
args = Airlock.pressurize(spaceship, args);
Airlock.open(spaceship, args);
};
for (i = 0, ln = _gaq.length; i < ln; i++) {
_gaq.push(_gaq[i]);
}
};
// Add tracker to Airlock, push settings to ga
Airlock.dock = function (spaceship) {
this.spaceships.set(spaceship.namespace, spaceship);
};
// Once our arguments are "pressurized", send them off to `ga()`
Airlock.open = function (spaceship, args) {
if (args) {
args[0] = !spaceship.namespace || args[0] === 'create' ?
args[0] :
[spaceship.namespace, args[0]].join('.');
window.ga.apply(window, args);
}
};
Airlock.pressurize = function (spaceship, args) {
var conversion = Airlock.conversions[args.splice(0,1)[0].replace(rx.actions, "$2")];
if (!conversion) { return; }
if (typeof conversion === 'function') {
return conversion.apply(spaceship, args);
}
var index = {}, out = [],
i = 0, ln = args.length < conversion.input.length ?
args.length :
conversion.input.length;
// build an index to match input labels to input values
for (; i < ln; i++) {
index[conversion.input[i]] = args[i];
}
// loop through outputs and permute values as necessary
i = 0; ln = conversion.output.length;
for (; i < ln; i++) {
var val = permute(conversion.output[i], index);
if (val) {
out.push(val);
}
}
return out;
};
var permute = function (template, data) {
if (typeof template === 'function') {
return template(data);
}
var templMatch = template.match(/\[\[([a-zA-Z_]+)\]\]/);
if (templMatch) {
if (!data[templMatch[1]]) { return; }
// if there is no input value corresponding to the template identifier
// (i.e., if this is an optional value), this will return undefined
// and will be ignored.
return template.replace(templMatch[0], data[templMatch[1]]);
}
return template;
};
var passthru = function (arg) { return arg; };
Airlock.conversions = {
// Setup actions
_setSampleRate: function (rate) {
this.settings.sampleRate = rate;
},
_setCookiePath: function (path) {
this.settings.cookiePath = path;
},
_set: function (key, val) {
this.settings[key.replace('ua', 'cookie')] = val;
if (rx.writeableSet.test(key)) {
return ['set',key,val];
}
},
_setDomainName: function (domainName) {
this._settings.domainName = domainName;
},
_setAllowLinker: function (allow) {
var that = this;
this.settings.allowLinker = allow;
this.setupQueue.push(function () {
window.ga('require', 'linker');
Airlock.open(that, ['linker:autoLink', that._settings.domainName]);
});
},
// Enhanced Link Attribution
_require: function (type){
if (type === 'inpage_linkid') {
return ['require','linkid','linkid.js'];
}
},
// Custom Variables
_setCustomVar: {
input: ['slot', 'name', 'value'],
output: ['set', 'dimension[[slot]]', '[[value]]']
},
// Tracking actions
_trackEvent: {
input: ['eventCategory', 'eventAction', 'eventLabel', 'eventValue', 'nonInteraction'],
output: ['send', 'event', passthru]
},
_trackPageview: {
input: ['pagePath'],
output: ['send', 'pageview', '[[pagePath]]']
},
_trackTiming: {
input: ['timingCategory', 'timingVar', 'timingValue', 'timingLabel'],
output: ['send', 'timing', passthru]
},
_trackSocial: {
input: ['socialNetwork', 'socialAction', 'socialTarget', 'page'],
output: ['send', 'social', passthru]
},
// Ecommerce
_addTrans: {
input: ['id', 'affiliation', 'revenue', 'tax', 'shipping'],
output: ['ecommerce:addTransaction', passthru]
},
_addItem: {
input: ['id', 'sku', 'name', 'category', 'price', 'quantity'],
output: ['ecommerce:addItem', passthru]
},
_trackTrans: {
input: [],
output: ['ecommerce:send']
}
};
Airlock.initialize();
window.Airlock = Airlock;
})(window, document);