forked from digitick/yii-google-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EGoogleAnalytics.php
187 lines (167 loc) · 5.45 KB
/
EGoogleAnalytics.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Google Analytics code generation widget
*
* @copyright © Digitick <www.digitick.net> 2012
* @license GNU Lesser General Public License v3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Lesser Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Google Analytics widget class.
*
* @author Ianaré Sévi
*/
class EGoogleAnalytics extends CApplicationComponent
{
/**
* @var string The full web property ID (e.g. UA-65432-1) for the tracker object.
*/
public $account;
/**
* @var boolean linker functionality flag as part of enabling cross-domain
* user tracking.
*/
public $allowLinker = true;
/**
* @var string the new cookie path for your site. By default, Google Analytics
* sets the cookie path to the root level (/).
*/
public $cookiePath;
/**
* @var string the domain name for the GATC cookies. Values: 'auto', 'none', [domain].
* [domain] is a specific domain, i.e. 'example.com'.
* 'auto' by default.
*/
public $domainName = 'auto';
/**
* @var array strings of ignored term(s) for Keywords reports.
*
* array('keyword1', 'keyword2')
*/
public $ignoredOrganics = array();
/**
* @var array Excludes a source as a referring site.
*
* array('referrer1', 'referrer2')
*/
public $ignoredRefs = array();
/**
* @var array Adds a search engine to be included as a potential search
* engine traffic source.
*
* array('engine1' => 'keyword2', 'engine2' => 'keyword2')
*/
public $organics = array();
/**
* @var boolean the browser tracking module flag.
*/
public $clientInfo = true;
/**
*
* @var boolean the Flash detection flag.
*/
public $detectFlash = true;
/**
*
* @var boolean the title detection flag.
*/
public $detectTitle = true;
/**
* @var array items purchased, multidimensional.
*
* array(
* array(
* 'orderId' => '1234',
* 'sku' => 'DD44',
* 'name' => 'T-Shirt',
* 'category' => 'Olive Medium',
* 'price' => '11.99',
* 'quantity' => '1'
* ),
* )
*/
public $items = array();
/**
* @var array commercial transactions, multidimensional.
*
* array(
* array(
* 'orderId' => '1234',
* 'affiliation' => 'Womens Apparel',
* 'total' => '28.28',
* 'tax' => '1.29',
* 'shipping' => '15.00',
* 'city' => 'San Jose',
* 'state' => 'California',
* 'country' => 'USA'
* ),
* )
*/
public $transactions = array();
/**
* @var string CClientScript position.
*/
public $position = CClientScript::POS_HEAD;
public function run()
{
if (!$this->account)
throw new CException('Google analytics account ID must be set.');
$ignoredOrganics = '';
foreach ($this->ignoredOrganics as $keyword) {
$ignoredOrganics .= "_gaq.push(['_addIgnoredOrganic', '$keyword']);\n";
}
$ignoredRefs = '';
foreach ($this->ignoredRefs as $referrer) {
$ignoredRefs .= "_gaq.push(['_addIgnoredRef', '$referrer']);\n";
}
$organics = '';
foreach ($this->organics as $engine => $keyword) {
$organics .= "_gaq.push(['_addOrganic','$engine', '$keyword']);\n";
}
$items = '';
foreach ($this->items as $item) {
$items .= "_gaq.push(['_addItem', '{$item['orderId']}', '{$item['sku']}', '{$item['name']}', '{$item['category']}', '{$item['price']}', '{$item['quantity']}']);\n";
}
$transactions = '';
foreach ($this->transactions as $trans) {
$transactions .= "_gaq.push(['_addTrans', '{$trans['orderId']}', '{$trans['affiliation']}', '{$trans['total']}', '{$trans['tax']}', '{$trans['shipping']}', '{$trans['city']}', '{$trans['state']}', '{$trans['country']}']);\n";
}
$trackTrans = '';
if (!empty($items) || !empty($transactions))
$trackTrans = "_gaq.push(['_trackTrans']);\n";
$script = "var _gaq = _gaq || [];\n";
$script .= "_gaq.push(['_setAccount', '{$this->account}']);\n";
$script .= $ignoredOrganics . $ignoredRefs . $organics;
$script .= "_gaq.push(['_setDomainName', '{$this->domainName}']);\n";
if ($this->cookiePath)
$script .= "_gaq.push(['_setCookiePath', '{$this->cookiePath}']);\n";
$script .= "_gaq.push(['_setAllowLinker', " . (($this->allowLinker) ? 'true' : 'false') . "]);\n";
if (!$this->clientInfo)
$script .= "_gaq.push(['_setClientInfo', false]);\n";
if (!$this->detectFlash)
$script .= "_gaq.push(['_setDetectFlash', false]);\n";
if (!$this->detectTitle)
$script .= "_gaq.push(['_setDetectTitle', false]);\n";
$script .= "_gaq.push(['_trackPageview']);\n";
$script .= $items . $transactions . $trackTrans;
$script .= "(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);
})();";
Yii::app()->getClientScript()->registerScript('google-analytics', $script, $this->position);
}
}