-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-analytics.lib.php
More file actions
153 lines (120 loc) · 4.26 KB
/
Copy pathgoogle-analytics.lib.php
File metadata and controls
153 lines (120 loc) · 4.26 KB
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
<?php
class GA {
const BASE_URL = "http://www.google-analytics.com/__utm.gif";
const ANALYTICS_VERSION = "4.3";
private $charset; // Charset
private $accountId; // Google Analytics account
private $language; // Language
private $hostName; // Host name (www.elements.at)
public function __construct($accountId, $charset = "UTF-8") {
$this->setAccountId($accountId);
$this->setCharset($charset);
$this->setHostName($_SERVER['SERVER_NAME']);
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$this->setLanguage(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
}
}
public function getAccountId() {
return $this->accountId;
}
public function setAccountId($accountId) {
$this->accountId = $accountId;
}
public function getHostName() {
return $this->hostName;
}
public function setHostName($hostName = "") {
$this->hostName = $hostName;
}
public function getCharset() {
return $this->charset;
}
public function setCharset($charset = "UTF-8") {
$this->charset = $charset;
}
public function getLanguage() {
return $this->language;
}
public function setLanguage($language = "en-us") {
$this->language = $language;
}
public function createEvent($category, $action, $label = "", $value = "") {
$parameters = array(
'utmwv' => self::ANALYTICS_VERSION,
'utmn' => $this->getGetUniqueId(),
'utmhn' => $this->getHostName(),
'utmt' => "event",
'utme' => $this->getEventString($category, $action, $label, $value),
'utmcs' => $this->getCharset(),
"utmul" => $this->getLanguage(),
"utmhid" => $this->getGetUniqueId(),
"utmac" => $this->getAccountId(),
"utmcc" => $this->getCookieVariables(),
"utmip" => $_SERVER["REMOTE_ADDR"]
);
return $this->requestHttp(self::BASE_URL, $parameters);
}
private function getGetUniqueId() {
return mt_rand(100000000, 999999999);
}
public function getEventString($category, $action, $label = "", $value = "") {
$value = (int) intval($value);
$eventString = "5(" . $category . "*" . $action;
if ($label) {
$eventString .= "*" . $label . ")";
} else {
$eventString .= ")";
}
if ($value) {
$eventString .= "(" . $value . ")";
}
return $eventString;
}
private function getCookieVariables() {
$cookie = $this->getGetUniqueId() . "00145214523";
$random = $this->getGetUniqueId();
$today = time();
return '__utma=1.' . $cookie . '.' . $random . '.' . $today . '.' . $today . '.15;+__utmz=1.' . $today . '.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);';
}
private function requestHttp($url, $getParams = array()) {
$client = curl_init();
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
curl_setopt($client, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($client, CURLOPT_HTTPHEADER, array("X-Forwarded-For: " . $_SERVER["REMOTE_ADDR"]));
curl_setopt($client, CURLOPT_URL, $url . "?" . http_build_query($getParams));
curl_exec($client);
curl_close($client);
}
}
class EventBuilder {
private $ga;
private $category;
private $action;
private $label = "";
private $value = "";
private function __construct(GA $ga) {
$this->ga = $ga;
}
public static function inctase($accountId, $charset = "UTF-8") {
return new EventBuilder(new GA($accountId, $charset));
}
public function withCategory($category) {
$this->category = $category;
return $this;
}
public function withAction($action) {
$this->action = $action;
return $this;
}
public function withLabel($label = "") {
$this->label = $label;
return $this;
}
public function withValue($value = "") {
$this->value = $value;
return $this;
}
public function createEvent() {
$this->ga->createEvent($this->category, $this->action, $this->label, $this->value);
}
}