forked from joaolcorreia/Google-Analytics-PHP-cookie-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.gaparse.php
More file actions
92 lines (71 loc) · 2.8 KB
/
class.gaparse.php
File metadata and controls
92 lines (71 loc) · 2.8 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
<?php
////////////////////////////////////////////////////
// GA_Parse - PHP Google Analytics Parser Class
//
// Version 1.0 - Date: 17 September 2009
// Version 1.1 - Date: 25 January 2012
// Version 1.2 - Date: 21 April 2012
//
// Define a PHP class that can be used to parse
// Google Analytics cookies currently with support
// for __utmz (campaign data) and __utma (visitor data)
//
// Author: Joao Correia - http://joaocorreia.pt
//
// License: LGPL
//
////////////////////////////////////////////////////
class GA_Parse
{
var $campaign_source; // Campaign Source
var $campaign_name; // Campaign Name
var $campaign_medium; // Campaign Medium
var $campaign_content; // Campaign Content
var $campaign_term; // Campaign Term
var $first_visit; // Date of first visit
var $previous_visit; // Date of previous visit
var $current_visit_started; // Current visit started at
var $times_visited; // Times visited
var $pages_viewed; // Pages viewed in current session
function __construct($cookie) {
if (isset($cookie["__utmz"])) {
$this->utmz = $cookie["__utmz"];
}
if (isset($cookie["__utma"])) {
$this->utma = $cookie["__utma"];
}
$this->ParseCookies();
}
function ParseCookies(){
// Parse __utmz cookie
list($domain_hash,$timestamp, $session_number, $campaign_numer, $campaign_data) = preg_split('[\.]', $_COOKIE['__utmz'],5);
// Parse the campaign data
$campaign_data = parse_str(strtr($campaign_data, '|', '&'));
$this->campaign_source = $utmcsr;
$this->campaign_name = $utmccn;
$this->campaign_medium = $utmcmd;
$this->campaign_term = (isset($utmctr) ? $utmctr : '');
$this->campaign_content = (isset($utmcct) ? $utmcct : '');
// If auto-tagging has been enabled in Google AdWords, we can infer some
// campaign parameters if they weren't defined manually
if (isset($utmgclid)) {
if (!isset($utmcsr)) $this->campaign_source = 'google';
if (!isset($utmcmd)) $this->campaign_medium = 'cpc';
}
// Parse the __utma Cookie
list($domain_hash,$random_id,$time_initial_visit,$time_beginning_previous_visit,$time_beginning_current_visit,$session_counter) = preg_split('[\.]', $_COOKIE['__utma']);
$this->first_visit = new \DateTime();
$this->first_visit->setTimestamp($time_initial_visit);
$this->previous_visit = new \DateTime();
$this->previous_visit->setTimestamp($time_beginning_previous_visit);
$this->current_visit_started = new \DateTime();
$this->current_visit_started->setTimestamp($time_beginning_current_visit);
$this->times_visited = $session_counter;
// Parse the __utmb Cookie
list($domain_hash,$pages_viewed,$garbage,$time_beginning_current_session) = preg_split('[\.]', $_COOKIE['__utmb']);
$this->pages_viewed = $pages_viewed;
// End ParseCookies
}
// End GA_Parse
}
?>