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

Action.php « LogStats « modules - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be5e3563198610fb5324088bf8f4abc121599486 (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
193
194
195
196
<?php

class Piwik_LogStats_Action
{
	
	 /*
	  * About the Action concept:
	  * 
	  * - An action is defined by a name.
	  * - The name can be specified in the JS Code in the variable 'action_name'
	  * - Handling UTF8 in the action name
	  * PLUGIN_IDEA - An action is associated to URLs and link to the URL from the interface
	  * PLUGIN_IDEA - An action hit by a visitor is associated to the HTML title of the page that triggered the action
	  * 
	  * + If the name is not specified, we use the URL(path+query) to build a default name.
	  *   For example for "http://piwik.org/test/my_page/test.html" 
	  *   the name would be "test/my_page/test.html"
	  * 
	  * We make sure it is clean and displayable.
	  * If the name is empty we set it to a default name.
	  * 
	  * TODO UTF8 handling to test
	  * 
	  * Specifications
	  *  
	  * - External file tracking
	  * 
	  *    * MANUAL Download tracking 
	  *      download = http://piwik.org/hellokity.zip
	  * 	(name = dir1/file alias name)
	  *
	  *    * AUTOMATIC Download tracking for a known list of file extensions. 
	  *    Make a hit to the piwik.php with the parameter: 
	  *      download = http://piwik.org/hellokity.zip
	  *  
	  *   When 'name' is not specified, 
	  * 	if AUTOMATIC and if anchor not empty => name = link title anchor
	  * 	else name = path+query of the URL
	  *   Ex: myfiles/beta.zip
	  *
	  * - External link tracking
	  * 
	  *    * MANUAL External link tracking
	  * 	 outlink = http://amazon.org/test
	  * 	(name = the big partners / amazon)
	  * 
	  *    * AUTOMATIC External link tracking
	  *      When a link is not detected as being part of the same website 
	  *     AND when the url extension is not detected as being a file download
	  * 	 outlink = http://amazon.org/test
	  * 
	  *  When 'name' is not specified, 
	  * 	if AUTOMATIC and if anchor not empty => name = link title anchor
	  * 	else name = URL
	  *   Ex: http://amazon.org/test
	  */
	private $actionName;
	private $url;
	private $defaultActionName;
	private $nameDownloadOutlink;
	
	const TYPE_ACTION   = 1;
	const TYPE_DOWNLOAD = 3;
	const TYPE_OUTLINK  = 2;
	
	function __construct( $db )
	{
		$this->actionName = Piwik_Common::getRequestVar( 'action_name', '', 'string');
		
		$downloadVariableName = Piwik_LogStats_Config::getInstance()->LogStats['download_url_var_name'];
		$this->downloadUrl = Piwik_Common::getRequestVar( $downloadVariableName, '', 'string');
		
		$outlinkVariableName = Piwik_LogStats_Config::getInstance()->LogStats['outlink_url_var_name'];
		$this->outlinkUrl = Piwik_Common::getRequestVar( $outlinkVariableName, '', 'string');
		
		$nameVariableName = Piwik_LogStats_Config::getInstance()->LogStats['download_outlink_name_var'];
		$this->nameDownloadOutlink = Piwik_Common::getRequestVar( $nameVariableName, '', 'string');
		
		$this->url = Piwik_Common::getRequestVar( 'url', '', 'string');
		$this->db = $db;
		$this->defaultActionName = Piwik_LogStats_Config::getInstance()->LogStats['default_action_name'];
	}
	
	private function generateInfo()
	{
		if(!empty($this->downloadUrl))
		{
			$this->actionType = self::TYPE_DOWNLOAD;
			$url = $this->downloadUrl;
			$actionName = $this->nameDownloadOutlink;
		}
		elseif(!empty($this->outlinkUrl))
		{
			$this->actionType = self::TYPE_OUTLINK;
			$url = $this->outlinkUrl;
			$actionName = $this->nameDownloadOutlink;
			if( empty($actionName) )
			{
				$actionName = $url;
			}
		}
		else
		{
			$this->actionType = self::TYPE_ACTION;
			$url = $this->url;
			$actionName = $this->actionName;
		}		
		
		// the ActionName wasn't specified
		if( empty($actionName) )
		{
			$parsedUrl = parse_url( $url );
			
			$actionName = '';
			
			if(isset($parsedUrl['path']))
			{
				$actionName .= substr($parsedUrl['path'], 1);
			}
			
			if(isset($parsedUrl['query']))
			{
				$actionName .= '?'.$parsedUrl['query'];
			}
		}
		
		// clean the name
		$actionName = str_replace(array("\n", "\r"), '', $actionName);
		
		if(empty($actionName))
		{
			$actionName = $this->defaultActionName;
		}
		
		$this->finalActionName = $actionName;
	}
	
	/**
	 * Returns the idaction of the current action name.
	 * This idaction is used in the visitor logging table to link the visit information 
	 * (entry action, exit action) to the actions.
	 * This idaction is also used in the table that links the visits and their actions.
	 * 
	 * The methods takes care of creating a new record in the action table if the existing 
	 * action name doesn't exist yet.
	 * 
	 * @return int Id action
	 */
	function getActionId()
	{
		$this->loadActionId();
		return $this->idAction;
	}
	
	/**
	 * @see getActionId()
	 */
	private function loadActionId()
	{		
		$this->generateInfo();
		
		$name = $this->finalActionName;
		$type = $this->actionType;
		
		$idAction = $this->db->fetch("	SELECT idaction 
							FROM ".$this->db->prefixTable('log_action')
						."  WHERE name = ? AND type = ?", array($name, $type) );
		
		// the action name has not been found, create it
		if($idAction === false)
		{
			$this->db->query("INSERT INTO ". $this->db->prefixTable('log_action'). "( name, type ) 
								VALUES (?,?)",array($name,$type) );
			$idAction = $this->db->lastInsertId();
		}
		else
		{
			$idAction = $idAction['idaction'];
		}
		
		$this->idAction = $idAction;
	}
	
	/**
	 * Records in the DB the association between the visit and this action.
	 */
	 public function record( $idVisit, $idRefererAction, $timeSpentRefererAction)
	 {
	 	$this->db->query("INSERT INTO ".$this->db->prefixTable('log_link_visit_action')
						." (idvisit, idaction, idaction_ref, time_spent_ref_action) VALUES (?,?,?,?)",
					array($idVisit, $this->idAction, $idRefererAction, $timeSpentRefererAction)
					);
	 }
}

?>