forked from djlosch/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapollo.js
168 lines (156 loc) · 6.44 KB
/
apollo.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
/*
Copyright (c) 2015 David Loschiavo, zerodarknerdy.com
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var APOLLO = APOLLO || (function() {
var scriptName = 'apollo.js';
var defaultID = 'apollo_adblock_placeholder';
var analytics = {};
var _args = {
'id' : defaultID,
'sampling' : 100, // percentage of pageviews that we'll test for adblocking
'verbose' : false, // do you want extra messaging to appear in console.log?
'timeout' : 100, // ms to wait before checking for ad blocking
'categoryBlocked' : 'adblock (apollo)', // GA category to be used when adblocking is detected
'categoryUnblocked' : 'adblock (apollo)', // GA category to be used when adblocking is NOT detected
'actionBlocked' : 'blocked', // GA action to be used when adblocking is detected
'actionUnblocked' : 'unblocked', // GA action to be used when adblocking is NOT detected
'labelBlocked' : window.location.pathname, // GA label to be used when adblocking is detected
'labelUnblocked' : window.location.pathname, // GA label to be used when adblocking is NOT detected
};
return {
init : function() {
// import args
var scripts = document.getElementsByTagName("script");
var i, j, src, parts, basePath;
var found = false;
var domElement;
for (i = 0; i < scripts.length; i++) {
src = scripts[i].src;
if (src.indexOf(scriptName) != -1) {
found = true;
parts = src.split('?');
basePath = parts[0].replace(scriptName, '');
if (parts[1]) {
var opt = parts[1].split('&');
for (j = opt.length-1; j >= 0; --j) {
var pair = opt[j].split('=');
_args[pair[0]] = pair[1];
}
}
break;
}
}
if (!found) {
console.log('adblocking analytics script has been renamed, and arguments are not detectable. please make sure you update "scriptName" in the source');
}
// sampling is off by default, but for high traffic sites, you may want to drop this down below 20 or you will hit daily datapoint limits in
if (_args.sampling < 100) {
var random = Math.floor((Math.random() * 100) + 1);
if (random > _args.sampling) {
_args.test = false;
if (_args.verbose)
console.log('skipping adblock test / sampling[' + _args.sampling + '%] rolled[' + random + ']');
return false;
}
}
if (_args.id == defaultID) {
if (_args.verbose)
console.log('using placeholder id[' + defaultID + ']');
domElement = document.createElement('div');
domElement.setAttribute("class", "advertisement ad advertising ad_holder")
domElement.setAttribute("id", defaultID)
domElement.setAttribute("style", "width:300px;height:250px;background:#CCC;top:-500px;position:absolute;");
document.body.appendChild(domElement);
} else {
if (_args.verbose)
console.log('not using placeholder id[' + defaultID + ']');
}
return true;
},
hasGoogleAnalyticsSupport : function() {
// ga() tracking in analytics.js... see https://developers.google.com/analytics/devguides/collection/analyticsjs/events
analytics.ga = true;
if (typeof window.ga != 'function') {
analytics.ga = false;
}
if (_args.verbose)
console.log('GA support via ga()[' + analytics.ga + ']');
// trackEvent tracking in ga.js... see https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide?hl=en
analytics.trackEvent = true;
if (typeof window.trackEvent != 'function') {
analytics.trackEvent = false;
}
if (_args.verbose)
console.log('GA support via trackEvent()[' + analytics.trackEvent + ']');
// some ultra-legacy implementations using ga.js weren't set up to use trackEvent() and need to gaq.push() instead.
analytics.gaq = true;
if (typeof _gaq == 'undefined') {
analytics.gaq = false;
}
if (_args.verbose)
console.log('GA support via _gaq[' + analytics.gaq + ']');
if (!analytics.ga && !analytics.trackEvent && !analytics.gaq) {
if (_args.verbose)
console.log('GA tracking is not available');
return false;
}
return true;
},
adblocked : function() {
if (_args.verbose)
console.log('starting adblock test');
var tag = document.getElementById(_args.id);
if ((tag.length < 1) || (tag.clientHeight < 1)) {
if (_args.verbose)
console.log('adblock on');
return true;
}
if (_args.verbose)
console.log('adblock off');
return false;
},
logToGoogleAnalytics : function(category, action, label) {
if (analytics.ga) {
ga('send', 'event', category, action, label);
} else if (analytics.trackEvent) {
trackEvent(category, action, label);
} else if (analytics.gaq) {
_gaq.push(['_trackEvent', category, action, label]);
}
},
getCategoryBlocked : function() { return _args.categoryBlocked; },
getActionBlocked : function() { return _args.actionBlocked; },
getLabelBlocked : function() { return _args.labelBlocked; },
getCategoryUnblocked : function() { return _args.categoryUnblocked; },
getActionUnblocked : function() { return _args.actionUnblocked; },
getLabelUnblocked : function() { return _args.labelUnblocked; },
}
}());
document.addEventListener("DOMContentLoaded", function (event) {
if (APOLLO.init()) {
setTimeout(function() {
if (APOLLO.hasGoogleAnalyticsSupport()) {
if (APOLLO.adblocked()) {
APOLLO.logToGoogleAnalytics(APOLLO.getCategoryBlocked(), APOLLO.getActionBlocked(), APOLLO.getLabelBlocked());
} else {
APOLLO.logToGoogleAnalytics(APOLLO.getCategoryUnblocked(), APOLLO.getActionUnblocked(), APOLLO.getLabelUnblocked());
}
}
}, APOLLO.timeout);
}
});