-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutboundtracking.php
162 lines (119 loc) · 4.21 KB
/
outboundtracking.php
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
<?php
/**
* @package GA Outbound Click Tracking - Plugin for Joomla 3.0!
* @author No More 404
* @copyright Copyright (c) 2012 NoMore404.nl
* @license MIT license: http://opensource.org/licenses/MIT
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin' );
class plgSystemOutboundTracking extends JPlugin {
function plgSystemOutboundTracking(&$subject, $params) {
parent::__construct($subject, $params);
$mode = $this->params->def('mode', 1);
}
function onAfterRender(){
$app =& JFactory::getApplication();
if( $app->isAdmin() ){
return;
}
$GAtrackingcode="";
$outboundtrackingscript="";
$buffer = JResponse::getBody();
$header = explode('</head>',$buffer);
if ($this->params->get('googleid')){
$GAtrackingcode .="
<script type='text/javascript'>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '".$this->params->get('googleid')."']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>";
}
//Declaring the debug stuff here to keep my JS clean and easy to read
if ( $this->params->get('debug')){
//Precaution in case console isn't declared
$debugaction1 = "
if( typeof(console) === 'undefined' ) {
var console = {}
console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
};
";
$debugaction2 = "console.log('Debug Mode On');";
$debugaction3 = "console.log('trying fallback microsoft CDN');";
$debugaction4 = "console.log('Error Loading jQuery from CDN');";
$debugaction5 = "console.log('GA Click Tracking Plugin Activated');";
$debugaction6 = "console.log('Click Event Registerd');";
}
$outboundtrackingscript .="
<script>
". $debugaction1 ."
// GA Outbound Cick Tracking - by No More 404
". $debugaction2 ."
if (typeof jQuery === 'undefined') {
loadjQuery('https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js', verifyJQueryCdnLoaded);
} else {
main();
}
function verifyJQueryCdnLoaded() {
if (typeof jQuery === 'undefined') {
loadjQuery('http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js', main);
". $debugaction3 ."
} else {
main();
}
}
function loadjQuery(url, callback) {
var script_tag = document.createElement('script');
script_tag.setAttribute('src', url)
script_tag.onload = callback;
script_tag.onreadystatechange = function() {
// Same but in IE style
if (this.readyState == 'complete' || this.readyState == 'loaded') callback();
}
script_tag.onerror = function() {
". $debugaction4 ."
}
document.getElementsByTagName('head')[0].appendChild(script_tag);
}
function main() {
if (typeof jQuery === 'undefined') {
throw 'ERROR: jQuery not loaded.';
}
(function($) {
$(function() {
var _gaq = _gaq || [];
". $debugaction5 ."
$('a').each(function() {
hostname = new RegExp(location.host);
// Local
if (hostname.test(this.href)) {
// Do Something
}
// Anchor
else if (this.href.slice(0, 1) == '#') {
// Anchor
}
// External
else {
$(this).click( function() {
_gaq.push(['_trackEvent', 'OutboundLink', 'Click', this.href]);
". $debugaction6 ."
});
}
});
});
}(jQuery));
}
</script>
";
$buffer = preg_replace ("/<\/body>/", "\n".$GAtrackingcode.$outboundtrackingscript."\n</body>", $buffer);
// $buffer = preg_replace ("/<\/head>/", "\n".."\n</head>", $buffer);
JResponse::setBody($buffer);
return;
}
}