Skip to content

Commit 3729c51

Browse files
committed
Multiple asynchronous queues fixed to keep working after snowplow has loaded
1 parent 3890eed commit 3729c51

3 files changed

Lines changed: 49 additions & 44 deletions

File tree

examples/web/async.html

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,41 @@
1414
-->
1515
<html>
1616
<head>
17-
<title>Asynchronous website/webapp examples for snowplow.js</title>
17+
<title>Asynchronous website/webapp examples for snowplow.js</title>localStorage.clear()
1818
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
1919

2020
<!-- Snowplow starts plowing -->
21-
<script type="text/javascript">
22-
window._snaq = window._snaq || [];
23-
24-
window._snaq.push(['setLinkTrackingTimer', 5000]);
25-
window._snaq.push(['setCollectorCf', 'd3rkrsqld9gmqf' ]); // Update to your CloudFront distribution subdomain
26-
window._snaq.push(['setAppId', 'CFe23a']); // Site ID can be anything you want. Set it if you're tracking more than one site in this account
27-
window._snaq.push(['setUserId', 'alex 123']); // Business-defined user ID
28-
//window._snaq.push(['setUserIdFromLocation', 'id']); // To test this, reload the page with ?id=xxx
29-
//window._snaq.push(['setUserIdFromCookie', '_sp_id.4209']) // Test this using Firefox because Chrome doesn't allow local cookies.
30-
window._snaq.push(['setCustomUrl', '/overridden-url/']); // Override the page URL
31-
window._snaq.push(['enableActivityTracking', 10, 10]); // Ping every 10 seconds after 10 seconds
32-
window._snaq.push(['enableLinkClickTracking', 'barred', true]);
33-
window._snaq.push(['setPlatform', 'mob']);
34-
window._snaq.push(['encodeBase64', false]); // Default is true
35-
//window._snaq.push(['enableUserFingerprint', false]); // Turn off user fingerprinting
36-
//window._snaq.push(['setUserFingerprintSeed', 987654321]); // Set user fingerprint hash seed
37-
window._snaq.push(['respectDoNotTrack', true]); // Prevent all tracking if browser's Do Not Track feature is enabled
38-
// window._snaq.push(['trackPageView', 'Async Test']); // Track the page view with custom title
39-
window._snaq.push(['trackPageView', , { // Auto-set page title; add page context
21+
<script type="text/javascript">
22+
23+
;(function(p,l,o,w,i,n,g) {
24+
p['GlobalSnowplowNamespace'] = p['GlobalSnowplowNamespace']||[];
25+
p['GlobalSnowplowNamespace'].push(i);
26+
p[i] = p[i] || function() {
27+
(p[i].q = p[i].q||[]).push(arguments);
28+
};
29+
p[i].q = p[i].q || [];
30+
n = l.createElement(o);
31+
g = l.getElementsByTagName(o)[0];
32+
n.async = 1;
33+
n.src = w;
34+
g.parentNode.insertBefore(n,g);
35+
}(window, document, 'script', '../../dist/snowplow.js', 'new_name_here'));
36+
37+
window.new_name_here('newTracker', 'primaryTrackerNamespace', 'd3rkrsqld9gmqf.cloudfront.net');
38+
39+
window.new_name_here('setAppId', 'CFe23a'); // Site ID can be anything you want. Set it if you're tracking more than one site in this account
40+
window.new_name_here('setUserId', 'alex 123'); // Business-defined user ID
41+
//window.new_name_here('setUserIdFromLocation', 'id'); // To test this, reload the page with ?id=xxx
42+
//window.new_name_here('setUserIdFromCookie', '_sp_id.4209') // Test this using Firefox because Chrome doesn't allow local cookies.
43+
window.new_name_here('setCustomUrl', '/overridden-url/'); // Override the page URL
44+
window.new_name_here('enableActivityTracking', 10, 10); // Ping every 10 seconds after 10 seconds
45+
window.new_name_here('enableLinkClickTracking', 'barred', true);
46+
window.new_name_here('setPlatform', 'mob');
47+
window.new_name_here('encodeBase64', false); // Default is true
48+
window.new_name_here('enableUserFingerprinting', true); // Turn on user fingerprinting
49+
window.new_name_here('respectDoNotTrack', true); // Prevent all tracking if browser's Do Not Track feature is enabled
50+
// window.new_name_here('trackPageView', 'Async Test'); // Track the page view with custom title
51+
window.new_name_here('trackPageView', null , { // Auto-set page title; add page context
4052
page: {
4153
page_type: 'test',
4254
last_updated: new Date(2014,1,26)
@@ -45,31 +57,25 @@
4557
user_type: 'tester',
4658

4759
}
48-
} ]);
49-
50-
(function() {
51-
var sp = document.createElement('script'); sp.type = 'text/javascript'; sp.async = true; sp.defer = true;
52-
//sp.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://d1fc8wv8zag5ca.cloudfront.net/0.13.0/sp.js';
53-
sp.src = '../../dist/snowplow.js'; // For testing
54-
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(sp, s);
55-
})();
60+
} );
61+
5662
</script>
5763
<!-- Snowplow stops plowing -->
5864

5965
<!-- Example events -->
6066
<script type="text/javascript">
6167

6268
function playMix() {
63-
alert("Playing a DJ mix");
64-
window._snaq.push(['trackStructEvent', 'Mixes', 'Play', 'MRC/fabric-0503-mix', '', '0.0']);
69+
alert("Playing a DJ mix"); console.log(window.new_name_here.q)
70+
window.new_name_here('trackStructEvent', 'Mixes', 'Play', 'MRC/fabric-0503-mix', '', '0.0');
6571
}
6672
function addProduct() {
6773
alert("Adding a product to basket");
68-
window._snaq.push(['trackStructEvent', 'Checkout', 'Add', 'ASO01043', 'blue:xxl', '2.0', { user: { fb_uid: '123456 x' } } ]);
74+
window.new_name_here('trackStructEvent', 'Checkout', 'Add', 'ASO01043', 'blue:xxl', '2.0', { user: { fb_uid: '123456 x' } } );
6975
}
7076
function viewProduct() {
7177
alert("Viewing a product");
72-
window._snaq.push(['trackUnstructEvent', 'Viewed Product',
78+
window.new_name_here('trackUnstructEvent', 'Viewed Product',
7379
{
7480
product_id: 'ASO01043',
7581
category: 'Dresses',
@@ -79,7 +85,7 @@
7985
sizes: ['xs', 's', 'l', 'xl', 'xxl'],
8086
available_since$dt: new Date(2013,3,7)
8187
}
82-
]);
88+
);
8389
}
8490

8591
function addEcommerceTransaction() {
@@ -88,7 +94,7 @@
8894
var orderId = 'order-123';
8995

9096
// addTrans sets up the transaction, should be called first.
91-
window._snaq.push(['addTrans',
97+
window.new_name_here('addTrans',
9298
orderId, // order ID - required
9399
'', // affiliation or store name
94100
'8000', // total - required
@@ -98,11 +104,11 @@
98104
'', // state or province
99105
'', // country
100106
'JPY' // currency
101-
]);
107+
);
102108

103109
// addItem might be called for each item in the shopping cart,
104110
// or not at all.
105-
window._snaq.push(['addItem',
111+
window.new_name_here('addItem',
106112
orderId, // order ID - required
107113
'1001', // SKU - required
108114
'Blue t-shirt', // product name
@@ -111,21 +117,21 @@
111117
'2', // quantity - required
112118
'JPY', // currency
113119
{ products: { "launch_date$tm": new Date(2013,3,7) } } // context
114-
]);
120+
);
115121

116-
window._snaq.push(['addItem',
122+
window.new_name_here('addItem',
117123
orderId, // order ID - required
118124
'1002', // SKU - required
119125
'Red shoes', // product name
120126
'', // category
121127
'4000', // unit price - required
122128
'1', // quantity - required
123129
'JPY' // currency
124-
]);
130+
);
125131

126132
// trackTrans sends the transaction to Snowplow tracking servers.
127133
// Must be called last to commit the transaction.
128-
window._snaq.push(['trackTrans']);
134+
window.new_name_here('trackTrans');
129135

130136
}
131137
</script>

src/js/in_queue.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@
136136
parameterArray = arguments[i];
137137

138138
// Arguments is not an array, so we turn it into one
139-
parameterArray = Array.prototype.slice.call(parameterArray, 0);
140-
inputString = parameterArray.shift();
139+
inputString = Array.prototype.shift.call(parameterArray);
141140
parsedString = parseInputString(inputString);
142141
f = parsedString[0];
143142
names = parsedString[1];

src/js/snowplow.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
function getAsynchronousQueue() {
114114
var queueName = windowAlias['GlobalSnowplowNamespace'].shift();
115115
var queue = windowAlias[queueName];
116-
return queue.q;
116+
return queue;
117117
}
118118

119119
/*
@@ -212,7 +212,7 @@
212212

213213
// Now replace initialization array with queue manager object
214214
var newQueue = getAsynchronousQueue();
215-
newQueue = new queue.InQueueManager(tracker.Tracker, version, mutSnowplowState, newQueue);
215+
newQueue.q = new queue.InQueueManager(tracker.Tracker, version, mutSnowplowState, newQueue.q);
216216

217217
/************************************************************
218218
* Public data and methods

0 commit comments

Comments
 (0)