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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CoreHome\Columns;
use Piwik\Metrics\Formatter;
use Piwik\Piwik;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Plugins\CoreHome\Segment;
use Piwik\Tracker\Action;
use Piwik\Tracker\GoalManager;
use Piwik\Tracker\Request;
use Piwik\Tracker\Visitor;
class VisitGoalBuyer extends VisitDimension
{
// log_visit.visit_goal_buyer
const TYPE_BUYER_NONE = 0;
const TYPE_BUYER_ORDERED = 1;
const TYPE_BUYER_OPEN_CART = GoalManager::TYPE_BUYER_OPEN_CART;
const TYPE_BUYER_ORDERED_AND_OPEN_CART = GoalManager::TYPE_BUYER_ORDERED_AND_OPEN_CART;
protected static $visitEcommerceStatus = array(
self::TYPE_BUYER_NONE => 'none',
self::TYPE_BUYER_ORDERED => 'ordered',
self::TYPE_BUYER_OPEN_CART => 'abandonedCart',
self::TYPE_BUYER_ORDERED_AND_OPEN_CART => 'orderedThenAbandonedCart',
);
protected $columnName = 'visit_goal_buyer';
protected $columnType = 'TINYINT(1) NULL';
protected $segmentName = 'visitEcommerceStatus';
protected $nameSingular = 'General_EcommerceVisitStatusDesc';
protected $type = self::TYPE_ENUM;
public function __construct()
{
$example = Piwik::translate('General_EcommerceVisitStatusEg', '"&segment=visitEcommerceStatus==ordered,visitEcommerceStatus==orderedThenAbandonedCart"');
$this->acceptValues = implode(", ", self::$visitEcommerceStatus) . '. ' . $example;
}
public function formatValue($value, $idSite, Formatter $formatter)
{
switch ($value) {
case 'none';
case '0':
case self::TYPE_BUYER_NONE:
return Piwik::translate('UserCountryMap_None');
case 'ordered':
case '1':
case self::TYPE_BUYER_ORDERED:
return Piwik::translate('CoreHome_VisitStatusOrdered');
case 'abandonedCart':
case self::TYPE_BUYER_OPEN_CART:
return Piwik::translate('Goals_AbandonedCart');
case 'orderedThenAbandonedCart':
case self::TYPE_BUYER_ORDERED_AND_OPEN_CART:
return Piwik::translate('CoreHome_VisitStatusOrderedThenAbandoned');
}
return $value;
}
public function getEnumColumnValues()
{
return self::$visitEcommerceStatus;
}
/**
* @param Request $request
* @param Visitor $visitor
* @param Action|null $action
* @return mixed
*/
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
return $this->getBuyerType($request);
}
/**
* @param Request $request
* @param Visitor $visitor
* @param Action|null $action
* @return int
*/
public function onExistingVisit(Request $request, Visitor $visitor, $action)
{
$goalBuyer = $visitor->getVisitorColumn($this->columnName);
// Ecommerce buyer status
$visitEcommerceStatus = $this->getBuyerType($request, $goalBuyer);
if ($visitEcommerceStatus != self::TYPE_BUYER_NONE
// only update if the value has changed (prevents overwriting the value in case a request has
// updated it in the meantime)
&& $visitEcommerceStatus != $goalBuyer) {
return $visitEcommerceStatus;
}
return false;
}
/**
* @ignore
*/
public static function getVisitEcommerceStatusFromId($id)
{
if (!isset(self::$visitEcommerceStatus[$id])) {
throw new \Exception("Unexpected ECommerce status value ");
}
return self::$visitEcommerceStatus[$id];
}
private function getBuyerType(Request $request, $existingType = self::TYPE_BUYER_NONE)
{
$isRequestEcommerce = $request->getMetadata('Ecommerce', 'isRequestEcommerce');
if (!$isRequestEcommerce) {
return $existingType;
}
$isGoalAnOrder = $request->getMetadata('Ecommerce', 'isGoalAnOrder');
if ($isGoalAnOrder) {
return self::TYPE_BUYER_ORDERED;
}
// request is Add to Cart
if ($existingType == self::TYPE_BUYER_ORDERED
|| $existingType == self::TYPE_BUYER_ORDERED_AND_OPEN_CART
) {
return self::TYPE_BUYER_ORDERED_AND_OPEN_CART;
}
return self::TYPE_BUYER_OPEN_CART;
}
}
|