1 | <?php |
||
19 | class GoogleAnalyticsTwigExtension extends AbstractExtension |
||
20 | { |
||
21 | protected $accountVarName = 'account_id'; |
||
22 | |||
23 | protected $accountId; |
||
24 | |||
25 | /** @var OrderPreparer */ |
||
26 | protected $orderPreparer; |
||
27 | |||
28 | /** @var OrderConverter */ |
||
29 | protected $orderConverter; |
||
30 | |||
31 | /** |
||
32 | * Returns a list of functions to add to the existing list. |
||
33 | * |
||
34 | * @return array An array of functions |
||
|
|||
35 | */ |
||
36 | public function getFunctions() |
||
37 | { |
||
38 | return array( |
||
39 | new TwigFunction( |
||
40 | 'google_analytics_initialize', |
||
41 | array($this, 'renderInitialize'), |
||
42 | array('is_safe' => array('html'), 'needs_environment' => true) |
||
43 | ), |
||
44 | new TwigFunction( |
||
45 | 'google_analytics_track_order', |
||
46 | array($this, 'renderECommerceTracking'), |
||
47 | array('is_safe' => array('html'), 'needs_environment' => true) |
||
48 | ), |
||
49 | ); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $id the Google Analytics Account ID |
||
54 | */ |
||
55 | public function setAccountID($id) |
||
56 | { |
||
57 | $this->accountId = $id; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param OrderPreparer $preparer |
||
62 | */ |
||
63 | public function setOrderPreparer($preparer) |
||
64 | { |
||
65 | $this->orderConverter = $preparer; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param OrderConverter $converter |
||
70 | */ |
||
71 | public function setOrderConverter($converter) |
||
72 | { |
||
73 | $this->orderConverter = $converter; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Renders the default Google Analytics JavaScript. |
||
78 | * |
||
79 | * If the options are not set it'll try and load the account ID from your parameters (google.analytics.account_id) |
||
80 | * |
||
81 | * @param Environment $environment |
||
82 | * @param array|null $options Example: {account_id: 'UA-XXXXX-Y'} |
||
83 | * |
||
84 | * @return string the HTML rendered |
||
85 | * |
||
86 | * @throws Error when the Google Analytics ID is nowhere to be found |
||
87 | */ |
||
88 | public function renderInitialize(Environment $environment, $options = null) |
||
89 | { |
||
90 | if (\is_null($options)) { |
||
91 | $options = array(); |
||
92 | } |
||
93 | |||
94 | $defaults = array(); |
||
95 | |||
96 | $this->setOptionIfNotSet($defaults, $this->accountVarName, $this->accountId); |
||
97 | |||
98 | // Things set in $options will override things set in $defaults. |
||
99 | $options = array_merge($defaults, $options); |
||
100 | |||
101 | if (!$this->isOptionSet($options, $this->accountVarName)) { |
||
102 | throw new RuntimeError("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'."); |
||
103 | } |
||
104 | |||
105 | $template = $environment->load('@KunstmaanSeo/GoogleAnalyticsTwigExtension/init.html.twig'); |
||
106 | |||
107 | return $template->render($options); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param Environment $environment |
||
112 | * @param Order $order |
||
113 | * |
||
114 | * @return string the HTML rendered |
||
115 | */ |
||
116 | public function renderECommerceTracking(Environment $environment, Order $order) |
||
126 | |||
127 | /** |
||
128 | * Prefer the given option if already set. Otherwise set the value given. |
||
129 | * |
||
130 | * @param array &$arr This is modified in place |
||
131 | * @param string $option the key in the $arr array |
||
132 | * @param mixed $value the new value if the option wasn't set already |
||
133 | */ |
||
134 | private function setOptionIfNotSet(&$arr, $option, $value) |
||
140 | |||
141 | /** |
||
142 | * Check if an option is set. |
||
143 | * |
||
144 | * @param array $arr the array to check |
||
145 | * @param string $option the key in the $arr array |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | private function isOptionSet($arr, $option) |
||
153 | } |
||
154 |
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.