1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\GoogleAnalytics\Tracking\Renderer\Script; |
4
|
|
|
|
5
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Data\Tracker; |
6
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Renderer\Script\AnalyticsJs\CreateCommand; |
7
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Renderer\Script\AnalyticsJs\Ecommerce\AddItemCommand; |
8
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Renderer\Script\AnalyticsJs\Ecommerce\AddTransactionCommand; |
9
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Renderer\Script\AnalyticsJs\RequireCommand; |
10
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Renderer\Script\AnalyticsJs\SendCommand; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AnalyticsJs |
14
|
|
|
* @package ByTIC\GoogleAnalytics\Tracking |
15
|
|
|
*/ |
16
|
|
|
class AnalyticsJs extends AbstractScript |
17
|
|
|
{ |
18
|
|
|
const DEFAULT_FUNCTION_NAME = 'ga'; |
19
|
|
|
|
20
|
|
|
protected $function = self::DEFAULT_FUNCTION_NAME; |
21
|
|
|
|
22
|
|
|
protected $loadedPlugins = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
protected function generateCode() |
28
|
|
|
{ |
29
|
|
|
$script = $this->generateLoadScript(); |
30
|
|
|
|
31
|
|
|
$trackers = $this->getGoogleAnalytics()->getTrackers(); |
32
|
|
|
|
33
|
|
|
foreach ($trackers as $alias => $tracker) { |
34
|
|
|
if ($tracker->isEnabled()) { |
35
|
|
|
$script .= $this->generateCreateTracker($tracker); |
36
|
|
|
$script .= $this->generateTransactions($tracker); |
37
|
|
|
$script .= $this->generateSend($tracker); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $script; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
protected function generateLoadScript() |
48
|
|
|
{ |
49
|
|
|
$script = <<<SCRIPT |
50
|
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
51
|
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
52
|
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
53
|
|
|
})(window,document,'script','//www.google-analytics.com/analytics.js','%s'); |
54
|
|
|
SCRIPT; |
55
|
|
|
|
56
|
|
|
return sprintf($script, $this->getFunctionName()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Tracker $tracker |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
protected function generateCreateTracker($tracker) |
64
|
|
|
{ |
65
|
|
|
return CreateCommand::generate($tracker, $this->getFunctionName()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Tracker $tracker |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function generateTransactions($tracker) |
73
|
|
|
{ |
74
|
|
|
$transactions = $tracker->getTransactions(); |
75
|
|
|
$hasTransactions = count($transactions) > 0; |
76
|
|
|
|
77
|
|
|
if (!$hasTransactions) { |
78
|
|
|
return ''; |
79
|
|
|
} |
80
|
|
|
$output = $this->requirePlugin($tracker, 'ecommerce'); |
81
|
|
|
|
82
|
|
|
foreach ($transactions as $transaction) { |
83
|
|
|
$output .= AddTransactionCommand::generate($tracker, $transaction, $this->getFunctionName()); |
84
|
|
|
$items = $transaction->getItems(); |
85
|
|
|
foreach ($items as $item) { |
86
|
|
|
$output .= AddItemCommand::generate($tracker, $item, $this->getFunctionName()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $output; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Tracker $tracker |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
protected function generateSend($tracker) |
98
|
|
|
{ |
99
|
|
|
return SendCommand::generate($tracker, $this->getFunctionName()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $name |
104
|
|
|
*/ |
105
|
|
|
public function setFunctionName($name) |
106
|
|
|
{ |
107
|
|
|
$this->function = $name; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getFunctionName() |
114
|
|
|
{ |
115
|
|
|
return $this->function; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Tracker $tracker |
120
|
|
|
* @param $name |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
protected function requirePlugin($tracker, $name) |
124
|
|
|
{ |
125
|
|
|
if (isset($this->loadedPlugins[$tracker->getAlias()][$name])) { |
126
|
|
|
return ''; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$params = []; |
130
|
|
|
|
131
|
|
|
$output = RequireCommand::generate($tracker, $name, $params, $this->getFunctionName()); |
132
|
|
|
$this->loadedPlugins[$tracker->getAlias()][$name] = true; |
133
|
|
|
return $output; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|