-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncSC.js
376 lines (359 loc) · 11.8 KB
/
asyncSC.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* ==========================================
* Asynchronous SiteCatalyst v0.2
* http://github.com/DieProduktMacher/asyncSC
* ==========================================
* Copyright:
* 2013 DieProduktMacher GmbH
*
* Authors:
* Tobias Kleyer <tobias.kleyer@produktmacher.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
_asc = (function(asc){
var to_return = {}; // object to return from anonymous function
var queue = asc; // the queue where all "tasks" are stored until given to the trackers
var loadedTrackers = {}; // Trackers used by this script. Currently there is only one.
///////////////////
// H E L P E R //
///////////////////
to_return.utils = {
/**
* helper function to remove duplicate entries from arrays
*
* @param arr an array with possible duplicate entries
* @return an array with all duplicates removed
*/
filter: function(arr){
var o = {};
var a = [];
for(var i = arr.length;i--;){
if(o.hasOwnProperty(arr[i])) {
continue;
}
a.push(arr[i]);
o[arr[i]] = 1;
}
return a;
},
/**
* Loads a script asynchronously and executes callback when finished
*
* @param url the full (or relative) path to the script to load
* @param callback the function to execute after the script is loaded
*/
getScript: function(url, callback) {
var h = document.getElementsByTagName('script')[0];
var el = document.createElement('script');
el.type = 'text/javascript';
el.async = true;
el.src = url;
// Handle Script loading
var done = false;
// Attach handlers for all browsers
el.onload = el.onreadystatechange = function(){
if ( !done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') ) {
done = true;
if (callback){
callback();
}
// Handle memory leak in IE
el.onload = el.onreadystatechange = null;
}
};
h.parentNode.insertBefore(el, h);
return undefined;
},
/**
* Combines two 1-dimensional json objects into one.
*
* @param target the object where the new properties should be mixed in
* @param source the object to include in the target object
* @param force determines if source properties should overwrite target properties (default: true)
* @return the combined object (note: the target is overwritten too)
*/
mixin: function(target, source, force){
force = force === false?false:true;
for(var src in source){
if(source.hasOwnProperty(src)){
if(force || !target.hasOwnProperty(src)){
target[src] = source[src];
}
}
}
return target;
},
/**
* Splits a url string in a window.loctation-like object
*
* @param url a complete url
*/
getLocation: function(url){
var ret = {};
var a = document.createElement('a');
a.href = url;
var fields = ['href','protocol','host','hostname','port','pathname','search','hash'];
for(var i = fields.length; i--; ){
ret[fields[i]] = a[fields[i]];
}
return ret;
}
};
/////////////////////////////
// R E Q U I R E M E N T S //
/////////////////////////////
/**
* Recursively handles all requirements given
*
* @param requirements an array of requirements
* @param callback function to call after all requirements are met
*/
var require = function(requirements, callback){
var that = this;
var args = arguments, shifted_req;
shifted_req = this.tracker.requirements[requirements.shift()];
if (requirements.length > 0) { // if more than on requirement left recursively call require
if (shifted_req[0]) {
args.callee.call(that, requirements, callback);
} else {
shifted_req[0] = true;
shifted_req[1]((function(){args.callee.call(that, requirements, callback);}));
}
} else if (shifted_req[0]) {
callback();
} else {
shifted_req[0] = true;
shifted_req[1](callback);
}
};
////////////////////////////
// T R A C K I N G A P I //
////////////////////////////
var Tracker = function(tracker){
this.tracker = tracker; // the tracker object
this.index = 0; // individual index of position in global queue
var that = this;
/**
* Handle single Queue Element. Pulls the first element from the queue and
* performs the task.
* Recursively calls itself until no tasks are left in the queue.
*
* @param (optional) queue an array of tasks
*/
var performTask = function(){
//queue = queue || that.queue;
var currentTask, currentTracker, currentRequirements;
if (that.index < queue.length) {
currentTask = queue[that.index];
that.index++;
if (typeof that.tracker[currentTask[0]] != 'undefined'){ // supported api function?
currentTracker = that.tracker[currentTask[0]];
if (currentTracker.length > 1) { // there are some requirements
that.workQueue = function(){}; // if external scripts contain _asc calls the task order breaks
currentRequirements = currentTracker.slice(1);
require.call(that, currentRequirements,
function(){
that.workQueue = function(){performTask();}; // restore
currentTracker[0].apply(that, currentTask.slice(1));
performTask(); // recusively call performTask
}
);
} else {
currentTracker[0].apply(that, currentTask.slice(1));
performTask(); // recusively call performTask
}
}
}
};
/**
* Run the next task for this tracker
*/
this.workQueue = function(){
performTask();
};
performTask();
};
/**
* Attaches a Tracker to listen on all tasks
*
* @param name the name of the tracker
* @param tracker the tracker closure
*/
to_return.addTracker = function(name, tracker){
loadedTrackers[name] = new Tracker(tracker);
//loadedTrackers[name].push.apply(loadedTrackers[name], queue);
//queue = [];
};
/*
* Function to send any tasks to the queue. The tasks are routed to the trackers afterwards
*/
to_return.push = function(){
for(var i = 0, l = arguments.length; i < l; i++){
queue.push(arguments[i]);
}
for(var t in loadedTrackers){
//loadedTrackers[t].push.apply(loadedTrackers[t], queue);
loadedTrackers[t].workQueue.apply(loadedTrackers[t]);
}
//queue = [];
};
return to_return;
})(_asc || []);
/*
* The SiteCatalyst Tracker
*/
_asc.addTracker('SiteCatalyst', (function(){
var sLocal = {}; // Store all SC Variables
var that = this; // For later reference
var additionalScripts = false; // contains the next script to be loaded
//////////////////////
// S C H E L P E R //
//////////////////////
/**
* This function is a wrapper around the complicated logic to use the native s.tl function
* of SiteCatalyst.
* It takes care of the configuration of linkTrackVars and linkTrackEvents
* @param obj A link obj to induce the 500ms delay or true to not use a delay
* @param lt The type of custom link tracking (o: custom, e: exits, d: downloads)
* @param ln The name of the link
* @param vars An object of SiteCatalyst variables to set
*/
var track = function(obj, lt, ln, vars){
var s = s_gi(window['s_account']);
var tempLinkVars = s.linkTrackVars;
var tempEvents = s.linkTrackEvents;
var params = s.linkTrackVars.toLowerCase() == "none" ? []: s.linkTrackVars.split(',');
var events = s.linkTrackEvents.toLowerCase() == "none" ? []: s.linkTrackEvents.split(',');
if(vars){
if(vars.hasOwnProperty('events')){
events.concat(vars['events'].split(','));
}
for(var key in vars){
if(vars.hasOwnProperty(key)){
key = key.replace(/^evar/,'eVar'); // Correct variable name if necessary
s[key] = vars[key];
params.push(key);
}
}
s.linkTrackVars = _asc.utils.filter(params).join(',');
s.linkTrackEvents = _asc.utils.filter(events).join(',');
}
s.tl(obj, lt, ln);
s.linkTrackVars = tempLinkVars;
s.linkTrackEvents = tempEvents;
};
/**
* Clears the local and global SC variable object.
*/
var clearSObject = function(){
sLocal = {}; // clear local object
var svar = window[to_return.config['sVariable']];
for(var i = 76; i--; ){
delete svar['prop'+i];
delete svar['eVar'+i];
delete svar['hier'+i];
delete svar['list'+i];
}
var properties = ['channel', 'pageName', 'campaign', 'server', 'referrer', 'zip', 'state', 'purchaseID', 'transactionID', 'events', 'pageType', 'products'];
for(i = properties.length; i--; ){
delete svar[properties[i]];
}
};
/**
* Returns the full path of the asyncSC.js
*/
var getDefaultScriptPath = function(){
var path = _asc.utils.getLocation(document.getElementById('_ascid').src);
return path.pathname.replace(/\/[^\/]*\.js$/i, '/s_code.js');
};
var to_return = {
////////////////////////////////
// D E F A U L T C O N F I G //
////////////////////////////////
config: {
disableTracking: false, // Disable all tracking
scodePath: getDefaultScriptPath(), // Path to the s_code.js script on your server
sVariable: 's', // Name of the SiteCatalyst Variable. The default is s.
account: ''
},
requirements: {
s_code: [false, function(callback){
_asc.utils.getScript(to_return.config.scodePath, callback);
}],
setAccount: [false, function(callback){
var svar = window[to_return.config['sVariable']];
svar.sa(to_return.config.account || window['s_account']);
callback();
}],
// this requirement can be called multiple times
additionalScripts: [true, function(callback){
_asc.utils.getScript(additionalScripts, callback);
}],
},
//////////////////////////
// T R A C K E R A P I //
//////////////////////////
setSCVariableName: [function(sVariable){
to_return.config['sVariable'] = sVariable;
}],
setCustomVars: [function(sVars){
sLocal = _asc.utils.mixin(sLocal, sVars);
}],
setConfig: [function(newConfig){
to_return.config = _asc.utils.mixin(to_return.config, newConfig);
}],
trackPageview: [function(){
if(to_return.config.disableTracking){return;}
var svar = window[to_return.config['sVariable']];
svar = _asc.utils.mixin(svar, sLocal);
svar.t(sLocal);
clearSObject();
}, 's_code', 'setAccount', 'additionalScripts'],
setAccount: [function(rsid){
var svar = window[to_return.config['sVariable']] || {};
if(to_return.requirements.s_code[0] && typeof svar.sa == 'function'){
svar.sa(rsid);
}
window['s_account'] = rsid;
to_return.config.account = rsid;
}],
disableTracking: [function(){
to_return.setConfig[0]({disableTracking: true});
}],
setScodePath: [function(path){
to_return.setConfig[0]({scodePath: path});
}],
trackClick: [function(linkName){
if(to_return.config.disableTracking){return;}
var arr = [true, 'o', linkName, sLocal];
track.apply(that, arr);
clearSObject();
}, 's_code', 'setAccount', 'additionalScripts'],
trackLink: [function(linkName, linkObj){
if(to_return.config.disableTracking){return;}
var a = linkObj || document.createElement('a');
a.href = a.href || linkName;
var arr = [a, 'o', linkName, sLocal];
track.apply(that, arr);
clearSObject();
}, 's_code', 'setAccount', 'additionalScripts'],
// loads scripts in the order given by the api. s_code is loaded first.
loadScript: [function(scriptPath){
additionalScripts = scriptPath;
to_return.requirements.additionalScripts[0] = false;
},'s_code', 'additionalScripts']
};
return to_return;
})());