Skip to content

Commit a4be7f6

Browse files
Piotr Limanowskipaulboocock
authored andcommitted
Add anonymized tracking options (close snowplow#793)
In order to activate anonymized user tracking following configurations can be used: - fully anonymized without any state nor session tracking ``` { anonymousTracking: true } ``` - anonymized with session tracking - session salt stored in the client ``` { anonymousTracking: {withSessionTracking: true } } ```
1 parent bbf23d4 commit a4be7f6

13 files changed

Lines changed: 588 additions & 111 deletions

core/npm-shrinkwrap.json

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/sp.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/web/cookieless.html

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!DOCTYPE html>
2+
3+
<!--
4+
! Copyright (c) 2012-2013 Snowplow Analytics Ltd. All rights reserved.
5+
!
6+
! This program is licensed to you under the Apache License Version 2.0,
7+
! and you may not use this file except in compliance with the Apache License Version 2.0.
8+
! You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
9+
!
10+
! Unless required by applicable law or agreed to in writing,
11+
! software distributed under the Apache License Version 2.0 is distributed on an
12+
! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
! See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
14+
-->
15+
<html>
16+
<head>
17+
<title>Cookieless tracking example for snowplow.js</title>
18+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
19+
20+
<!-- Snowplow starts plowing -->
21+
<script type="text/javascript">
22+
;(function(p, l, o, w, i, n, g) {
23+
if (!p[i]) {
24+
p.GlobalSnowplowNamespace = p.GlobalSnowplowNamespace || []
25+
p.GlobalSnowplowNamespace.push(i)
26+
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+
}
36+
})(window, document, 'script', '../sp.js', 'snowplow')
37+
38+
window.snowplow('newTracker', 'cf', '127.0.0.1:9090', {
39+
// Initialise a tracker
40+
anonymousTracking: true,
41+
encodeBase64: false, // Default is true
42+
appId: 'CFe23a', // Site ID can be anything you want. Set it if you're tracking more than one site in this account
43+
platform: 'mob',
44+
contexts: {
45+
webPage: true,
46+
gaCookies: true,
47+
performanceTiming: true
48+
}
49+
})
50+
</script>
51+
<!-- Snowplow stops plowing -->
52+
</head>
53+
54+
<body>
55+
<header>
56+
<h1>Cookieless_tracking_examples_for_snowplow.js</h1>
57+
58+
<p>
59+
Warning: if your browser's Do Not Track feature is enabled and
60+
respectDoNotTrack is enabled, all tracking will be prevented.
61+
</p>
62+
<p>
63+
If you are viewing the page using a file URL, you must edit the script
64+
URL in the Snowplow tag to include an http scheme. Otherwise a file
65+
scheme will be inferred and the page will attempt to load sp.js from the
66+
local filesystem..
67+
</p>
68+
</header>
69+
70+
<nav>
71+
<div id="login-form" style="display:block">
72+
<span class="fontawesome-user"></span>
73+
<input type="text" id="user" placeholder="Username" />
74+
75+
<span class="fontawesome-lock"></span>
76+
<input type="password" id="pass" placeholder="Password" />
77+
78+
<input type="submit" value="Login" onclick="login()" />
79+
</div>
80+
</nav>
81+
82+
<section>
83+
<div id="logout-form" style="display:none">
84+
<button type="button" onclick="logout()">Logout</button>
85+
</div>
86+
</section>
87+
<section>
88+
<button type="button" onclick="playMix()">Play a Mix</button>
89+
</section>
90+
91+
<script>
92+
function playMix() {
93+
alert('Playing a DJ mix')
94+
window.snowplow(
95+
'trackStructEvent',
96+
'Mixes',
97+
'Play',
98+
'MRC/fabric-0503-mix',
99+
'',
100+
'0.0'
101+
)
102+
}
103+
104+
function login() {
105+
window.snowplow('enableUserTracking', 'localStorage')
106+
window.snowplow('setUserId', 'alex 123') // Business-defined user ID
107+
window.snowplow('setUserIdFromLocation', 'id') // To test this, reload the page with ?id=xxx
108+
window.snowplow('setUserIdFromCookie', '_sp_id.4209') // Test this using Firefox because Chrome doesn't allow local cookies.
109+
window.snowplow('setCustomUrl', '/overridden-url/') // Override the page URL
110+
window.snowplow('enableActivityTracking', 10, 10) // Ping every 10 seconds after 10 seconds
111+
112+
toggle()
113+
return false
114+
}
115+
116+
function logout() {
117+
window.snowplow('disableUserTracking', 'localStorage')
118+
toggle()
119+
120+
return false
121+
}
122+
123+
function toggle() {
124+
var login = document.getElementById('login-form')
125+
var logout = document.getElementById('logout-form')
126+
127+
if (login.style.display == 'block') {
128+
login.style.display = 'none'
129+
logout.style.display = 'block'
130+
} else {
131+
login.style.display = 'block'
132+
logout.style.display = 'none'
133+
}
134+
}
135+
</script>
136+
</body>
137+
</html>

npm-shrinkwrap.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"dependencies": {
55
"jstimezonedetect": "1.0.5",
66
"sha1": "git://github.com/pvorb/node-sha1.git#910081c83f3661507d9d89e66e3f38d8b59d5559",
7-
"snowplow-tracker-core": "^0.7.2",
7+
"snowplow-tracker-core": "^0.8.0",
88
"uuid": "^3.3.3"
99
},
1010
"devDependencies": {

src/js/lib/helpers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,22 @@
347347
}
348348
};
349349

350+
/**
351+
* Attempt to delete a value from localStorage
352+
*
353+
* @param string key
354+
* @return boolean Whether the operation succeeded
355+
*/
356+
object.attemptDeleteLocalStorage = function (key) {
357+
try {
358+
localStorage.removeItem(key);
359+
localStorage.removeItem(key + '.expires');
360+
return true;
361+
} catch(e) {
362+
return false;
363+
}
364+
};
365+
350366
/**
351367
* Attempt to get a value from sessionStorage
352368
*

0 commit comments

Comments
 (0)