Skip to content

Commit 4e0f1bc

Browse files
committed
Made JS invocation tag part of the build process (snowplow#158)
1 parent a17f349 commit 4e0f1bc

6 files changed

Lines changed: 114 additions & 32 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ node_modules
2121
lib_managed
2222
dist
2323
tests/pages/*.js
24+
tags/tag.min.js
2425

2526
# creds for Grunt
2627
aws.json

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Fixed link to code climate button in README.md (#149)
2626
Added examples of tracker namespacing (#159)
2727
Split async.html into async-small.html, async-medium.html (#160)
2828
Linked the Technical Docs and Setup Guide images to the appropriate pages (#164)
29+
Made JS invocation tag part of the build process (#158)
2930

3031
Version 1.0.3 (2014-06-27)
3132
--------------------------

Gruntfile.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ module.exports = function(grunt) {
110110
'process': true
111111
},
112112
src: ['dist/bundle.js'],
113-
114113
dest: 'dist/snowplow.js'
114+
},
115+
tag: {
116+
options: {
117+
banner: ';'
118+
},
119+
src: ['tags/tag.min.js'],
120+
dest: 'tags/tag.min.js'
115121
}
116122
},
117123

@@ -123,8 +129,19 @@ module.exports = function(grunt) {
123129
},
124130
files: [
125131
{
126-
src: 'dist/snowplow.js',
127-
dest: 'dist/sp.js'
132+
src: 'dist/snowplow.js',
133+
dest: 'dist/sp.js'
134+
}
135+
]
136+
},
137+
tag: {
138+
options: {
139+
linebreak: 50
140+
},
141+
files: [
142+
{
143+
src: 'tags/tag.js',
144+
dest: 'tags/tag.min.js'
128145
}
129146
]
130147
}
@@ -226,9 +243,10 @@ module.exports = function(grunt) {
226243
});
227244
});
228245

229-
grunt.registerTask('default', 'Build lodash, Browserify, add banner, and minify', ['lodash', 'browserify:main', 'concat', 'min']);
230-
grunt.registerTask('publish', 'Upload to S3 and invalidate Cloudfront (full semantic version only)', ['upload_setup', 'concat', 'min', 's3:not_pinned', 'invalidate_cloudfront:not_pinned']);
231-
grunt.registerTask('publish-pinned', 'Upload to S3 and invalidate Cloudfront (full semantic version and semantic major version)', ['upload_setup', 'concat', 'min', 's3', 'invalidate_cloudfront']);
246+
grunt.registerTask('default', 'Build lodash, Browserify, add banner, and minify', ['lodash', 'browserify:main', 'concat:dist', 'min:dist']);
247+
grunt.registerTask('publish', 'Upload to S3 and invalidate Cloudfront (full semantic version only)', ['upload_setup', 'concat:dist', 'min:dist', 's3:not_pinned', 'invalidate_cloudfront:not_pinned']);
248+
grunt.registerTask('publish-pinned', 'Upload to S3 and invalidate Cloudfront (full semantic version and semantic major version)', ['upload_setup', 'concat:dist', 'min:dist', 's3', 'invalidate_cloudfront']);
232249
grunt.registerTask('test', 'Intern tests', ['browserify:test', 'intern']);
233250
grunt.registerTask('travis', 'Intern tests for Travis CI', ['lodash','browserify:test','intern']);
251+
grunt.registerTask('tags', 'Minifiy the Snowplow invocation tag', ['min:tag', 'concat:tag']);
234252
}

examples/web/async-medium.html

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,11 @@
2020
<!-- Snowplow starts plowing -->
2121
<script type="text/javascript">
2222

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'));
23+
// Load sp.js
24+
;(function(p,l,o,w,i,n,g){if(!p[i]){p.GlobalSnowplowNamespace=p.GlobalSnowplowNamespace||[];
25+
p.GlobalSnowplowNamespace.push(i);p[i]=function(){(p[i].q=p[i].q||[]).push(arguments)
26+
};n=l.createElement(o);g=l.getElementsByTagName(o)[0];
27+
n.async=1;n.src=w;g.parentNode.insertBefore(n,g)}}(window,document,"script","../../dist/snowplow.js","new_name_here"));
3628

3729
window.new_name_here('newTracker', 'cf', 'd3rkrsqld9gmqf.cloudfront.net', {
3830
encodeBase64: false,

examples/web/async-small.html

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,11 @@
2020
<!-- Snowplow starts plowing -->
2121
<script type="text/javascript">
2222

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', 'snowplow'));
23+
// Load sp.js
24+
;(function(p,l,o,w,i,n,g){if(!p[i]){p.GlobalSnowplowNamespace=p.GlobalSnowplowNamespace||[];
25+
p.GlobalSnowplowNamespace.push(i);p[i]=function(){(p[i].q=p[i].q||[]).push(arguments)
26+
};n=l.createElement(o);g=l.getElementsByTagName(o)[0];
27+
n.async=1;n.src=w;g.parentNode.insertBefore(n,g)}}(window,document,"script","../../dist/snowplow.js","snowplow"));
3628

3729
window.snowplow('newTracker', 'cf', 'd3rkrsqld9gmqf.cloudfront.net', { // Initialise a tracker
3830
encodeBase64: false, // Default is true

tags/tag.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* JavaScript tracker for Snowplow: tag.js
3+
*
4+
* Significant portions copyright 2010 Anthon Pang. Remainder copyright
5+
* 2012-2014 Snowplow Analytics Ltd. All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are
9+
* met:
10+
*
11+
* * Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* * Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in the
16+
* documentation and/or other materials provided with the distribution.
17+
*
18+
* * Neither the name of Anthon Pang nor Snowplow Analytics Ltd nor the
19+
* names of their contributors may be used to endorse or promote products
20+
* derived from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
/*
36+
* Use this function to load Snowplow
37+
*
38+
* @param p The window
39+
* @param l The document
40+
* @param o "script", the tag name of script elements
41+
* @param w string The source of the Snowplow script. Make sure you get the latest version.
42+
* @param i string The Snowplow namespace. The Snowplow user should set this.
43+
* @param n The new script (to be created inside the function)
44+
* @param g The first script on the page (to be found inside the function)
45+
*/
46+
;(function(p,l,o,w,i,n,g) {
47+
"p:nomunge, l:nomunge, o:nomunge, w:nomunge, i:nomunge, n:nomunge, g:nomunge";
48+
49+
// Stop if the Snowplow namespace i already exists
50+
if (!p[i]) {
51+
52+
// Initialise the 'GlobalSnowplowNamespace' array
53+
p['GlobalSnowplowNamespace'] = p['GlobalSnowplowNamespace'] || [];
54+
55+
// Add the new Snowplow namespace to the global array so sp.js can find it
56+
p['GlobalSnowplowNamespace'].push(i);
57+
58+
// Create the asynchronous queue
59+
p[i] = function() {
60+
(p[i].q = p[i].q || []).push(arguments);
61+
};
62+
63+
// Create a new script element
64+
n = l.createElement(o);
65+
66+
// Get the first script on the page
67+
g = l.getElementsByTagName(o)[0];
68+
69+
// The new script should load asynchronously
70+
n.async = 1;
71+
72+
// Load Snowplow
73+
n.src = w;
74+
75+
// Insert the Snowplow script before every other script so it executes as soon as possible
76+
g.parentNode.insertBefore(n,g);
77+
}
78+
} (window, document, 'script', '../../dist/snowplow.js', 'new_name_here'));

0 commit comments

Comments
 (0)