Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GoalManager.php « Tracker « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ecb1f1353b3b91e1baf9b11fd49a244dc7747e52 (plain)
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
187
188
189
190
191
192
<?php

class Piwik_Tracker_GoalManager 
{
	/**
	 * @var Piwik_Cookie 
	 */
	protected $cookie = null;
	/**
	 * @var Piwik_Tracker_Action
	 */
	protected $action = null;
	protected $matchedGoals = array();
	protected $idsite = null;
	
	function __construct($action)
	{
		$this->action = $action;
		
		
	}

	function setCookie($cookie)
	{
		$this->cookie = $cookie;
	}

	static public function getGoalDefinitions( $idSite )
	{
		$websiteAttributes = Piwik_Common::getCacheWebsiteAttributes( $idSite );
		if(isset($websiteAttributes['goals']))
		{
			return $websiteAttributes['goals'];
		}
		return array();
	}
	
	static public function getGoalDefinition( $idSite, $idGoal )
	{
		$goals = self::getGoalDefinitions( $idSite );
		foreach($goals as $goal)
		{
			if($goal['idgoal'] == $idGoal)
			{
				return $goal;
			}
		}
		throw new Exception("The goal id = $idGoal couldn't be found.");
	}
	
	static public function getGoalIds( $idSite )
	{
		$goals = self::getGoalDefinitions( $idSite );
		$goalIds = array();
		foreach($goals as $goal)
		{
			$goalIds[] = $goal['idgoal'];
		}
		return $goalIds;
	}
	
	//TODO does this code work for manually triggered goals, with custom revenue? 
	function detectGoals($idSite)
	{
		if(!Piwik_PluginsManager::getInstance()->isPluginActivated('Goals'))
		{
			return false;
		}
		$url = $this->action->getActionUrl();
		$actionType = $this->action->getActionType();
		$goals = $this->getGoalDefinitions($idSite);
		foreach($goals as $goal)
		{
			$attribute = $goal['match_attribute'];
			// if the attribute to match is not the type of the current action
			if(		($actionType == Piwik_Tracker_Action::TYPE_ACTION && $attribute != 'url')
				||	($actionType == Piwik_Tracker_Action::TYPE_DOWNLOAD && $attribute != 'file')
				||	($actionType == Piwik_Tracker_Action::TYPE_OUTLINK && $attribute != 'external_website')
				)
			{
				continue;
			}
			
			$pattern_type = $goal['pattern_type'];
			
			switch($pattern_type)
			{
				case 'regex':
					$pattern = '/' . $goal['pattern'] . '/';
					if(!$goal['case_sensitive'])
					{
						$pattern .= 'i';
					}
					$match = (preg_match($pattern, $url) == 1);
					break;
				case 'contains':
					if($goal['case_sensitive'])
					{
						$matched = strpos($url, $goal['pattern']);
					}
					else
					{
						$matched = stripos($url, $goal['pattern']);
					}
					$match = ($matched !== false);
					break;
				case 'exact':
					if($goal['case_sensitive'])
					{
						$matched = strcmp($goal['pattern'], $url);
					}
					else
					{
						$matched = strcasecmp($goal['pattern'], $url);
					}
					$match = ($matched == 0);
					break;
				default:
					throw new Exception("Pattern type $pattern_type not valid.");
					break;
			}
			if($match)
			{
				$this->matchedGoals[] = $goal;
			}
		}
//		var_dump($this->matchedGoals);exit;
		return count($this->matchedGoals) > 0;
	}
	
	function recordGoals($visitorInformation)
	{
		$location_country = isset($visitorInformation['location_country']) ? $visitorInformation['location_country'] : Piwik_Common::getCountry(Piwik_Common::getBrowserLanguage(), $enableLanguageToCountryGuess = Piwik_Tracker_Config::getInstance()->Tracker['enable_language_to_country_guess']);
		$location_continent = isset($visitorInformation['location_continent']) ? $visitorInformation['location_continent'] : Piwik_Common::getContinent($location_country);
		
		$goal = array(
			'idvisit' 			=> $visitorInformation['idvisit'],
			'idsite' 			=> $visitorInformation['idsite'],
			'visitor_idcookie' 	=> $visitorInformation['visitor_idcookie'],
			'server_time' 		=> Piwik_Tracker::getDatetimeFromTimestamp($visitorInformation['visit_last_action_time']),
			'visit_server_date' => $visitorInformation['visit_server_date'],
			'idaction' 			=> $this->action->getIdAction(),
			'idlink_va' 		=> $this->action->getIdLinkVisitAction(),
			'location_country'  => $location_country,
			'location_continent'=> $location_continent,
			'url' 				=> $this->action->getActionUrl(),
			'visitor_returning' => $this->cookie->get( Piwik_Tracker::COOKIE_INDEX_VISITOR_RETURNING ),
		);

		$referer_idvisit = $this->cookie->get(  Piwik_Tracker::COOKIE_INDEX_REFERER_ID_VISIT );
		if($referer_idvisit !== false)
		{
			$goal += array(
				'referer_idvisit' 			=> $referer_idvisit,
				'referer_visit_server_date' => date("Y-m-d", $this->cookie->get( Piwik_Tracker::COOKIE_INDEX_REFERER_TIMESTAMP )),
				'referer_type' 				=> htmlspecialchars_decode($this->cookie->get( Piwik_Tracker::COOKIE_INDEX_REFERER_TYPE )),
				'referer_name' 				=> htmlspecialchars_decode($this->cookie->get(  Piwik_Tracker::COOKIE_INDEX_REFERER_NAME )),
				'referer_keyword' 			=> htmlspecialchars_decode($this->cookie->get(  Piwik_Tracker::COOKIE_INDEX_REFERER_KEYWORD )),
			);
		}

		foreach($this->matchedGoals as $matchedGoal)
		{
			printDebug("- Goal ".$matchedGoal['idgoal'] ." matched. Recording...");
			$newGoal = $goal;
			$newGoal['idgoal'] = $matchedGoal['idgoal'];
			$newGoal['revenue'] = $matchedGoal['revenue'];
			printDebug($newGoal);
			
			$fields = implode(", ", array_keys($newGoal));
			$bindFields = substr(str_repeat( "?,",count($newGoal)),0,-1);
			
			try {
				Piwik_Tracker::getDatabase()->query(
					"INSERT INTO " . Piwik_Common::prefixTable('log_conversion') . "	($fields) 
					VALUES ($bindFields) ", array_values($newGoal) 
				);
			} catch( Exception $e) {
				if(strpos($e->getMessage(), '1062') !== false)
				{
					// integrity violation when same visit converts to the same goal twice
					printDebug("--> Goal already recorded for this (idvisit, idgoal)");
				}
				else
				{
					throw $e;
				}
			}
			//$idlog_goal = Piwik_Tracker::getDatabase()->lastInsertId();
		}
	}
}