Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit f8ecf56

Browse files
author
Thomas Deinhamer
committed
Merge branch 'release/0.2.0'
2 parents 52c86d1 + 62ab3b4 commit f8ecf56

File tree

5 files changed

+76
-13
lines changed

5 files changed

+76
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
## [next]
44

5+
## [0.2.0] - 2016-02-21
6+
7+
### Added
8+
- Add support for DNT.
9+
510
## [0.1.0] - 2016-02-04
611

712
### Added
813
- Initial release
914

10-
[next]: https://github.com/thasmo/gaoo.js/compare/v0.1.0...HEAD
15+
[next]: https://github.com/thasmo/gaoo.js/compare/v0.2.0...HEAD
16+
[0.2.0]: https://github.com/thasmo/gaoo.js/compare/v0.1.0...v0.2.0
1117
[0.1.0]: https://github.com/thasmo/gaoo.js/compare/6a775835e85ecf763257abe3322b282ba171b5ad...v0.1.0

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
55
`gaoo.js` is a small helper script to [opt-out][ga-optout] from Google Analytics Tracking.
66

7-
It uses local storage to remember an opt-out and will set a `window`
8-
property which Google Analytics [inspects][ga-optout] to decide whether to track or not.
7+
It uses local storage to remember an opt-out and will set a `window` property which Google Analytics
8+
[inspects][ga-optout] to decide whether to track or not. It supports `DNT` and respects it by default,
9+
if not configured otherwise.
910

1011
## Usage
1112

1213
```js
13-
// Initialize on page load. This needs to be called before ga.js is executed.
14+
// Initialize on page load with respect to DNT.
1415
window.gaoo('UA-123456-1');
1516

17+
// ... initialize on page load and do not respect DNT.
18+
window.gaoo('UA-123456-1', false);
19+
1620
// Actively opt-out from Google Analytics tracking.
1721
window.gaoo('UA-123456-1').enable();
1822

@@ -21,11 +25,16 @@ window.gaoo('UA-123456-1').disable();
2125

2226
// Check if currently opted out.
2327
if(window.gaoo('UA-123456-1').check()) {
24-
window.console.log('Currently opted out.');
28+
window.console.info('Currently opted out.');
29+
} else {
30+
window.console.info('Tracking page-view now.');
2531
}
2632

2733
// Get opt-out identification string.
2834
var identifier = window.gaoo('UA-123456-1').identifier;
35+
36+
// Get DNT browser setting.
37+
window.console.log(window.gaoo('UA-123456-1').dnt()); // true or false
2938
```
3039

3140
### Examples
@@ -61,3 +70,5 @@ Currently it's not possible to define multiple properties to opt-out from.
6170

6271
**Can domains be configured to opt-out from?**
6372
Opting out works for the current `protocol://host:port` combination only.
73+
74+
[ga-optout]: https://developers.google.com/analytics/devguides/collection/analyticsjs/user-opt-out

bower.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "gaoo.js",
3+
"description": "Simple Google Analytics Opt-Out helper.",
4+
"keywords": ["google", "analytics", "opt-out", "opt", "out", "tracking"],
5+
"main": "src/gaoo.js",
6+
"authors": [
7+
"Thomas Deinhamer <thasmo@gmail.com>"
8+
],
9+
"license": "MIT",
10+
"homepage": "https://github.com/thasmo/gaoo.js",
11+
"moduleType": ["globals"],
12+
"ignore": [
13+
"**/.*",
14+
"node_modules",
15+
"bower_components",
16+
"test",
17+
"tests"
18+
]
19+
}

dist/gaoo.min.js

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

src/gaoo.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,57 @@
11
(function (window) {
22
'use strict';
33

4-
var gaoo = function (key) {
4+
var gaoo = function (key, respectDNT) {
55
var identifier = 'ga-disable-' + key;
66
var storageKey = 'gaoo';
77

8+
respectDNT = respectDNT || true;
9+
10+
var initialize = function () {
11+
!loaded() && persist(respectDNT && dnt());
12+
update(check());
13+
};
14+
15+
var loaded = function () {
16+
return window.localStorage.getItem(storageKey) !== null;
17+
};
18+
19+
var persist = function (value) {
20+
window.localStorage.setItem(storageKey, value);
21+
};
22+
23+
var update = function (value) {
24+
window[identifier] = value;
25+
};
26+
827
var enable = function () {
9-
window[identifier] = true;
10-
window.localStorage.setItem(storageKey, true);
28+
update(true);
29+
persist(true);
1130
};
1231

1332
var disable = function () {
14-
window[identifier] = false;
15-
window.localStorage.removeItem(storageKey);
33+
update(false);
34+
persist(false);
1635
};
1736

1837
var check = function () {
19-
return window.localStorage.getItem(storageKey) !== null;
38+
return window.localStorage.getItem(storageKey);
39+
};
40+
41+
var dnt = function () {
42+
return window.navigator.doNotTrack == '1' ||
43+
window.navigator.doNotTrack == 'yes' ||
44+
window.navigator.msDoNotTrack == '1' ||
45+
window.doNotTrack == '1';
2046
};
2147

22-
window[identifier] = window[identifier] || check();
48+
initialize();
2349

2450
return {
2551
enable: enable,
2652
disable: disable,
2753
check: check,
54+
dnt: dnt,
2855
identifier: identifier
2956
};
3057
};

0 commit comments

Comments
 (0)