@@ -35,6 +35,7 @@ class HookDisplayBeforeBodyClosingTag implements HookInterface
3535{
3636 private $ module ;
3737 private $ context ;
38+ private $ gaScripts = '' ;
3839
3940 public function __construct (Ps_Googleanalytics $ module , Context $ context )
4041 {
@@ -49,13 +50,35 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
4950 */
5051 public function run ()
5152 {
53+ // Prepare our tag handler
5254 $ gaTagHandler = new GanalyticsJsHandler ($ this ->module , $ this ->context );
55+
56+ // Add events for item listing
57+ $ this ->renderProductListing ();
58+
59+ // Add events for search
60+ $ this ->renderSearch ();
61+
62+ // Add events for search
63+ $ this ->renderCartPage ();
64+
65+ // Render begin checkout
66+ $ this ->renderBeginCheckout ();
67+
68+ // TODO
69+ // Sign_up event after registration, we need to check if the register was submitted
70+ // Login event, we need to check if the login was done in this request
71+ // Cart actions adding/removing
72+
73+ return $ gaTagHandler ->generate ($ this ->gaScripts );
74+ die;
75+
76+
5377 $ ganalyticsDataHandler = new GanalyticsDataHandler (
5478 $ this ->context ->cart ->id ,
5579 $ this ->context ->shop ->id
5680 );
5781
58- $ gaScripts = '' ;
5982 $ gacarts = $ ganalyticsDataHandler ->manageData ('' , 'R ' );
6083 $ controller_name = Tools::getValue ('controller ' );
6184
@@ -125,24 +148,160 @@ public function run()
125148 $ ganalyticsDataHandler ->manageData ('' , 'D ' );
126149 }
127150
151+ return $ gaTagHandler ->generate ($ gaScripts );
152+ }
153+
154+ /**
155+ * This method renders tracking code for product listings, like category pages.
156+ *
157+ * @return string
158+ */
159+ private function renderProductListing ()
160+ {
161+ // Try to get product list variable
128162 $ listing = $ this ->context ->smarty ->getTemplateVars ('listing ' );
163+ if (empty ($ listing ['products ' ])) {
164+ return ;
165+ }
166+
167+ // Prepare items to our format
129168 $ productWrapper = new ProductWrapper ($ this ->context );
130- $ products = $ productWrapper ->wrapProductList (isset ($ listing ['products ' ]) ? $ listing ['products ' ] : []);
131-
132- if ($ controller_name == 'order ' || $ controller_name == 'orderopc ' ) {
133- $ eventData = [
134- 'currency ' => $ this ->context ->currency ->iso_code ,
135- ];
136- $ gaScripts = $ this ->module ->getTools ()->renderEvent (
137- 'begin_checkout ' ,
138- $ eventData
139- );
169+ $ items = [];
170+ $ counter = 0 ;
171+ foreach ($ listing ['products ' ] as $ product ) {
172+ $ product = $ productWrapper ->prepareItemFromProductLazyArray ($ product );
173+ $ product ['index ' ] = $ counter ;
174+ $ items [] = $ product ;
175+ $ counter ++;
140176 }
141177
142- if (isset ($ products ) && count ($ products ) && $ controller_name != 'index ' ) {
143- $ gaScripts .= $ this ->module ->getTools ()->addProductClick ($ products , $ this ->context ->currency ->iso_code );
178+ // Prepare info about the list
179+ $ item_list_id = $ this ->context ->controller ->php_self ;
180+ $ item_list_name = $ listing ['label ' ];
181+
182+ // Render the listing event
183+ $ eventData = [
184+ 'item_list_id ' => $ item_list_id ,
185+ 'item_list_name ' => $ item_list_name ,
186+ 'items ' => $ items ,
187+ ];
188+ $ this ->gaScripts .= $ this ->module ->getTools ()->renderEvent (
189+ 'view_item_list ' ,
190+ $ eventData
191+ );
192+ }
193+
194+ /**
195+ * This method renders tracking code when user searches on the shop.
196+ */
197+ private function renderSearch ()
198+ {
199+ // Check if we are on search page and we have a search string
200+ if ($ this ->context ->controller ->php_self != 'search ' || empty ($ _GET ['s ' ])) {
201+ return ;
144202 }
145203
146- return $ gaTagHandler ->generate ($ gaScripts );
204+ // Render the listing event
205+ $ eventData = [
206+ 'search_term ' => (string ) $ _GET ['s ' ],
207+ ];
208+ $ this ->gaScripts .= $ this ->module ->getTools ()->renderEvent (
209+ 'search ' ,
210+ $ eventData
211+ );
212+ }
213+
214+ /**
215+ * This method renders tracking code for product listings, like category pages.
216+ *
217+ * @return string
218+ */
219+ private function renderCartpage ()
220+ {
221+ // Check if we are on cart page
222+ if ($ this ->context ->controller ->php_self != 'cart ' ) {
223+ return ;
224+ }
225+
226+ // Try to get product list variable and check if it's not empty
227+ $ cart = $ this ->context ->smarty ->getTemplateVars ('cart ' );
228+ if (empty ($ cart ['products ' ])) {
229+ return ;
230+ }
231+
232+ // Prepare items to our format
233+ $ productWrapper = new ProductWrapper ($ this ->context );
234+ $ items = [];
235+ $ counter = 0 ;
236+ foreach ($ cart ['products ' ] as $ product ) {
237+ $ product = $ productWrapper ->prepareItemFromProductLazyArray ($ product );
238+ $ product ['index ' ] = $ counter ;
239+ $ items [] = $ product ;
240+ $ counter ++;
241+ }
242+
243+ // Render the listing event
244+ $ eventData = [
245+ 'currency ' => $ this ->context ->currency ->iso_code ,
246+ 'value ' => $ cart ['totals ' ]['total ' ]['amount ' ],
247+ 'items ' => $ items ,
248+ ];
249+ $ this ->gaScripts .= $ this ->module ->getTools ()->renderEvent (
250+ 'view_cart ' ,
251+ $ eventData
252+ );
253+ }
254+
255+ /**
256+ * This method renders tracking code for product listings, like category pages.
257+ *
258+ * @return string
259+ */
260+ private function renderBeginCheckout ()
261+ {
262+ // Check if we are on some supported order controller
263+ $ allowed_controllers = ['order ' , 'orderopc ' , 'checkout ' ];
264+ if (!in_array ($ this ->context ->controller ->php_self , $ allowed_controllers )) {
265+ return ;
266+ }
267+
268+ // If using default OrderController that comes with prestashop, we will check if we are
269+ // on step 1 of the checkout. Otherwise, we will flush the output anyway. It's probably OPC
270+ // handling everything with javascript, so our code will load only once.
271+ if (get_class ($ this ->context ->controller ) == "OrderController " ) {
272+ // If we are not in the first step of checkout, we don't do anything
273+ // TODO test how it behaves with logged in customer
274+ if (!$ this ->context ->controller ->getCheckoutProcess ()->getSteps ()[0 ]->isCurrent ()) {
275+ return ;
276+ }
277+ }
278+
279+ // Try to get product list variable and check if it's not empty
280+ $ cart = $ this ->context ->smarty ->getTemplateVars ('cart ' );
281+ if (empty ($ cart ['products ' ])) {
282+ return ;
283+ }
284+
285+ // Prepare items to our format
286+ $ productWrapper = new ProductWrapper ($ this ->context );
287+ $ items = [];
288+ $ counter = 0 ;
289+ foreach ($ cart ['products ' ] as $ product ) {
290+ $ product = $ productWrapper ->prepareItemFromProductLazyArray ($ product );
291+ $ product ['index ' ] = $ counter ;
292+ $ items [] = $ product ;
293+ $ counter ++;
294+ }
295+
296+ // Render the listing event
297+ $ eventData = [
298+ 'currency ' => $ this ->context ->currency ->iso_code ,
299+ 'value ' => $ cart ['totals ' ]['total ' ]['amount ' ],
300+ 'items ' => $ items ,
301+ ];
302+ $ this ->gaScripts .= $ this ->module ->getTools ()->renderEvent (
303+ 'begin_checkout ' ,
304+ $ eventData
305+ );
147306 }
148307}
0 commit comments