1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\SeoBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\SeoBundle\Helper\Order, |
6
|
|
|
Kunstmaan\SeoBundle\Helper\OrderConverter, |
7
|
|
|
Kunstmaan\SeoBundle\Helper\OrderPreparer; |
8
|
|
|
|
9
|
|
|
use Twig_Environment, |
10
|
|
|
Twig_Extension; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Twig extensions for Google Analytics |
14
|
|
|
*/ |
15
|
|
|
class GoogleAnalyticsTwigExtension extends Twig_Extension |
16
|
|
|
{ |
17
|
|
|
protected $accountVarName = 'account_id'; |
18
|
|
|
|
19
|
|
|
protected $accountId; |
20
|
|
|
|
21
|
|
|
/** @var OrderPreparer */ |
22
|
|
|
protected $orderPreparer; |
23
|
|
|
|
24
|
|
|
/** @var OrderConverter */ |
25
|
|
|
protected $orderConverter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns a list of functions to add to the existing list. |
29
|
|
|
* |
30
|
|
|
* @return array An array of functions |
|
|
|
|
31
|
|
|
*/ |
32
|
|
View Code Duplication |
public function getFunctions() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
return array( |
35
|
|
|
new \Twig_SimpleFunction( |
36
|
|
|
'google_analytics_initialize', |
37
|
|
|
array($this, 'renderInitialize'), |
38
|
|
|
array('is_safe' => array('html'), 'needs_environment' => true) |
39
|
|
|
), |
40
|
|
|
new \Twig_SimpleFunction( |
41
|
|
|
'google_analytics_track_order', |
42
|
|
|
array($this, 'renderECommerceTracking'), |
43
|
|
|
array('is_safe' => array('html'), 'needs_environment' => true) |
44
|
|
|
), |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $id The Google Analytics Account ID. |
51
|
|
|
*/ |
52
|
|
|
public function setAccountID($id) |
53
|
|
|
{ |
54
|
|
|
$this->accountId = $id; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param OrderPreparer $preparer |
59
|
|
|
*/ |
60
|
|
|
public function setOrderPreparer($preparer) |
61
|
|
|
{ |
62
|
|
|
$this->orderConverter = $preparer; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param OrderConverter $converter |
67
|
|
|
*/ |
68
|
|
|
public function setOrderConverter($converter) |
69
|
|
|
{ |
70
|
|
|
$this->orderConverter = $converter; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Renders the default Google Analytics JavaScript. |
75
|
|
|
* |
76
|
|
|
* If the options are not set it'll try and load the account ID from your parameters (google.analytics.account_id) |
77
|
|
|
* |
78
|
|
|
* @param $environment \Twig_Environment |
79
|
|
|
* @param $options array|null Example: {account_id: 'UA-XXXXX-Y'} |
80
|
|
|
* |
81
|
|
|
* @return string The HTML rendered. |
82
|
|
|
* |
83
|
|
|
* @throws \Twig_Error_Runtime When the Google Analytics ID is nowhere to be found. |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
|
|
public function renderInitialize(\Twig_Environment $environment, $options = null) |
87
|
|
|
{ |
88
|
|
|
if (is_null($options)) { |
89
|
|
|
$options = array(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$defaults = array(); |
93
|
|
|
|
94
|
|
|
$this->setOptionIfNotSet($defaults, $this->accountVarName, $this->accountId); |
95
|
|
|
|
96
|
|
|
// Things set in $options will override things set in $defaults. |
97
|
|
|
$options = array_merge($defaults, $options); |
98
|
|
|
|
99
|
|
|
if (!$this->isOptionSet($options, $this->accountVarName)) { |
100
|
|
|
throw new \Twig_Error_Runtime( |
101
|
|
|
"The google_analytics_initialize function depends on a Google Analytics account ID. You can either pass this along in the initialize_google_analytics function ($this->accountVarName), provide a variable under 'parameters.google.analytics.account_id'." |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$template = $environment->loadTemplate('KunstmaanSeoBundle:GoogleAnalyticsTwigExtension:init.html.twig'); |
106
|
|
|
|
107
|
|
|
return $template->render($options); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Twig_Environment $environment |
113
|
|
|
* @param Order $order |
114
|
|
|
* |
115
|
|
|
* @return string The HTML rendered. |
116
|
|
|
*/ |
117
|
|
|
public function renderECommerceTracking(\Twig_Environment $environment, Order $order) |
118
|
|
|
{ |
119
|
|
|
$order = $this->orderPreparer->prepare($order); |
120
|
|
|
$options = $this->orderConverter->convert($order); |
121
|
|
|
$template = $environment->loadTemplate( |
122
|
|
|
'KunstmaanSeoBundle:GoogleAnalyticsTwigExtension:ecommerce_tracking.html.twig' |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $template->render($options); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Prefer the given option if already set. Otherwise set the value given. |
130
|
|
|
* |
131
|
|
|
* @param array &$arr This is modified in place. |
132
|
|
|
* @param string $option The key in the $arr array. |
133
|
|
|
* @param mixed $value The new value if the option wasn't set already. |
134
|
|
|
*/ |
135
|
|
|
private function setOptionIfNotSet(&$arr, $option, $value) |
136
|
|
|
{ |
137
|
|
|
if ($this->isOptionSet($arr, $option)) { |
138
|
|
|
$arr[$option] = $value; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check if an option is set. |
144
|
|
|
* |
145
|
|
|
* @param array $arr The array to check. |
146
|
|
|
* @param string $option The key in the $arr array. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
private function isOptionSet($arr, $option) |
151
|
|
|
{ |
152
|
|
|
return (!isset($arr[$option]) || !empty($arr[$option])); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.