This repository has been archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
class.TrafficTracker.php
182 lines (170 loc) · 8.87 KB
/
class.TrafficTracker.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
<?php
/* ==========================================================
* class.TrafficTracker.php v1.7
* https://github.com/adamdehaven/TrafficTracker
*
* Author: Adam Dehaven ( @adamdehaven )
* http://about.adamdehaven.com/
*
* ==========================================================
* MIT License
*
* Copyright (c) 2013 Adam Dehaven
*
* 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.
* ========================================================== */
class TrafficTracker
{
/* -------------------------------------------------------------------------
------------------------------- SET DEFAULTS -------------------------------
--------------------------------------------------------------------------*/
private $urlPrefix = 'http'; // Set URL prefix for your website.
private $replaceInUrl = array('?customer=new','?version=mobile'); // strip out any custom strings from URL.
private $myIp = array('10.0.0.1'); // Put IP addresses you would like to filter out (not track) in array.
private $reportingTimezone = 'America/Kentucky/Louisville'; // http://www.php.net/manual/en/timezones.america.php
private $dateFormat = 'Y-m-d H:i:s'; // Preferred date format - http://php.net/manual/en/function.date.php
private $cookieExpire = 30; // Set number of days for AdWords tracking cookies to be valid.
/* =========================================================================
============================= DO NOT EDIT BELOW ============================
==========================================================================*/
private $dbHost;
private $dbUsername;
private $dbPassword;
private $dbDatabase;
private $trackPrefix;
private $deleteRollingDays;
private $referrerMedium;
private $referrerSource;
private $referrerContent;
private $referrerCampaign;
private $referrerKeyword;
private $referrerAdwordsKeyword;
private $referrerAdwordsMatchType;
private $referrerAdwordsPosition;
private $identifyUser;
private $firstVisit;
private $previousVisit;
private $currentVisitStarted;
private $timesVisited;
private $pagesViewed;
private $theCurrentUrl;
private $referrerPageViewed;
private $userIp;
private $visitTimestamp;
private function setAdwordsCookies() {
if(isset($_GET[$this->trackPrefix]) && $_GET[$this->trackPrefix] == 'true'):
// Set cookie indicating referrer is Google AdWords Click good for 30 days
setcookie($this->trackPrefix.'_referrer', 'googleAdwords', $cookieDie,'/');
setcookie($this->trackPrefix.'_kw', $_GET[$this->trackPrefix.'_kw'], $cookieDie,'/');
setcookie($this->trackPrefix.'_pos', $_GET[$this->trackPrefix.'_pos'], $cookieDie,'/');
setcookie($this->trackPrefix.'_mt', $_GET[$this->trackPrefix.'_mt'], $cookieDie,'/');
endif;
} //-- end setAdwordsCookies()
private function processDefaults() {
$dirtyUrl = $this->urlPrefix.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; // Get URL of the current page.
$this->theCurrentUrl = str_replace($replaceInUrl, '', $dirtyUrl); // delete unwanted strings from URL.
$this->visitTimestamp = date($this->dateFormat); // Set Current Date & Time
$this->userIp = $_SERVER['REMOTE_ADDR']; // Set Visitor's IP Address
} //--end processDefaults()
private function parseCookies() {
// Parse __utmz cookie
list($domainHash,$sourceTimestamp, $sessionNumber, $campaignNumber, $campaignData) = explode('.', $_COOKIE["__utmz"],5);
$sourceData = parse_str(strtr($campaignData, '|', '&')); // Parse the __utmz data
$this->referrerMedium = $utmcmd; // medium (organic, referral, direct, etc)
$this->referrerSource = $utmcsr; // source (google, facebook.com, etc)
$this->referrerContent = $utmcct; // content (index.html, etc)
$this->referrerCampaign = $utmccn; // campaign
$this->referrerKeyword = $utmctr; // term (search term)
$this->referrerPageViewed = $this->theCurrentUrl;
if(isset($utmgclid)): // if from AdWords
$this->referrerSource = 'google';
$this->referrerCampaign = '';
$this->referrerMedium = 'cpc';
$this->referrerContent = '';
$this->referrerAdwordsKeyword = $utmctr;
endif;
// Parse the __utma Cookie
list($domainHash,$uniqueId,$timestampFirstVisit,$timestampPreviousVisit,$timestampStartCurrentVisit,$numSessionsStarted) = explode('.', $_COOKIE["__utma"]);
$this->identifyUser = $uniqueId; // Get Google Analytics unique user ID.
setcookie($this->trackPrefix.'_id', $this->identifyUser, $cookieDie,'/'); // Set Unique ID to $trackPrefix_id cookie.
$this->firstVisit = date($this->dateFormat,$timestampFirstVisit); // Get timestamp of first visit.
$this->previousVisit = date($this->dateFormat,$timestampPreviousVisit); // Get timestamp of previous visit.
$this->currentVisitStarted = date($this->dateFormat,$timestampStartCurrentVisit); // Get timestamp of current visit.
$this->timesVisited = $numSessionsStarted; // Get number of times visited.
// Parse the __utmb Cookie
list($domainHash,$pageViews,$garbage,$timestampStartCurrentVisit) = explode('.', $_COOKIE['__utmb']);
$this->pagesViewed = $pageViews; // Get the total number of page views.
} //-- end parseCookies()
// If user came from AdWords
private function setIfAdWords() {
$this->referrerAdwordsKeyword = (isset($_COOKIE[$this->trackPrefix.'_kw']) ? $_COOKIE[$this->trackPrefix.'_kw'] : $utmctr); // Set adwordsKeyword
$this->referrerAdwordsMatchType = (isset($_COOKIE[$this->trackPrefix.'_mt']) ? $_COOKIE[$this->trackPrefix.'_mt'] : ''); // Set adwordsMatchType
$this->referrerAdwordsPosition = (isset($_COOKIE[$this->trackPrefix.'_pos']) ? $_COOKIE[$this->trackPrefix.'_pos'] : ''); // Set adwordsPosition
} //-- end setIfAdWords()
private function logTraffic() { // Write to Database
error_reporting(0);
$mysqli = new mysqli($this->dbHost,$this->dbUsername,$this->dbPassword,$this->dbDatabase); // Connect to database
if ($mysqli->connect_error):
die('Connect Error (' . $mysqli->connect_errno . ') '.$mysqli->connect_error);
endif;
if(isset($this->referrerPageViewed) && !in_array($this->userIp,$this->myIp)): // If referrerPageViewed is set & not an internal IP
$mysqli->query("INSERT INTO trafficTracker (identifyUser, medium, source, content, campaign, keyword, pageViewed, adwordsKeyword, adwordsMatchType, adwordsPosition, firstVisit, previousVisit, currentVisit, timesVisited, pagesViewed, userIp, timestamp) VALUES
(
'".$this->identifyUser."',
'".$this->referrerMedium."',
'".$this->referrerSource."',
'".$this->referrerContent."',
'".$this->referrerCampaign."',
'".$this->referrerKeyword."',
'".$this->referrerPageViewed."',
'".$this->referrerAdwordsKeyword."',
'".$this->referrerAdwordsMatchType."',
'".$this->referrerAdwordsPosition."',
'".$this->firstVisit."',
'".$this->previousVisit."',
'".$this->currentVisitStarted."',
'".$this->timesVisited."',
'".$this->pagesViewed."',
'".$this->userIp."',
'".$this->visitTimestamp."'
)"
);
endif;
$mysqli->query("DELETE FROM trafficTracker WHERE timestamp < DATE_SUB(NOW(), INTERVAL ".$this->deleteRollingDays." DAY)");
$mysqli->close();
} //-- end logTraffic()
function __construct($dbHost,$dbUsername,$dbPassword,$dbDatabase,$trackPrefix='ttcpc',$deleteRollingDays=30) {
$this->dbHost = $dbHost;
$this->dbUsername = $dbUsername;
$this->dbPassword = $dbPassword;
$this->dbDatabase = $dbDatabase;
$this->trackPrefix = $trackPrefix;
$this->deleteRollingDays = "$deleteRollingDays";
date_default_timezone_set($this->reportingTimezone); // Set timezone.
$cookieDie = time() + ($this->cookieExpire * 24 * 60 * 60); // Cookie is good for X Number of days; 24 hours; 60 mins; 60secs
$this->setAdwordsCookies(); // Set cookies for AdWords
// If we have the cookies - parse them
if(isset($_COOKIE['__utma']) && isset($_COOKIE['__utmz'])):
$this->processDefaults();
$this->parseCookies();
$this->setIfAdWords();
$this->logTraffic();
endif;
} //-- end __construct($_COOKIE)
} //-- end class TrafficTracker
?>