@@ -53,22 +53,21 @@ public function run()
5353 // Prepare our tag handler
5454 $ gaTagHandler = new GanalyticsJsHandler ($ this ->module , $ this ->context );
5555
56- // Add events for item listing
57- $ this ->renderProductListing ();
56+ // Log information about product listing
57+ $ this ->saveInformationAboutListing ();
5858
59- // Add events for search
59+ // Add events
60+ $ this ->renderProductListing ();
6061 $ this ->renderSearch ();
61-
62- // Add events for search
6362 $ this ->renderCartPage ();
64-
65- // Render begin checkout
6663 $ this ->renderBeginCheckout ();
64+ $ this ->renderLogin ();
65+ $ this ->renderRegistration ();
6766
6867 // 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
68+ // Unify wrappers - add proper quantities for cart items
7169 // Cart actions adding/removing
70+ // Shipping info
7271
7372 return $ gaTagHandler ->generate ($ this ->gaScripts );
7473 die;
@@ -83,8 +82,6 @@ public function run()
8382 $ controller_name = Tools::getValue ('controller ' );
8483
8584 if (count ($ gacarts ) > 0 && $ controller_name != 'product ' ) {
86- $ this ->module ->filterable = 0 ;
87-
8885 foreach ($ gacarts as $ key => $ gacart ) {
8986 if (isset ($ gacart ['quantity ' ])) {
9087 if ($ gacart ['quantity ' ] > 0 ) {
@@ -153,8 +150,6 @@ public function run()
153150
154151 /**
155152 * This method renders tracking code for product listings, like category pages.
156- *
157- * @return string
158153 */
159154 private function renderProductListing ()
160155 {
@@ -166,20 +161,13 @@ private function renderProductListing()
166161
167162 // Prepare items to our format
168163 $ productWrapper = new ProductWrapper ($ this ->context );
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 ++;
176- }
177-
164+ $ items = $ productWrapper ->prepareItemListFromProductList ($ listing ['products ' ]);
165+
178166 // Prepare info about the list
179167 $ item_list_id = $ this ->context ->controller ->php_self ;
180168 $ item_list_name = $ listing ['label ' ];
181169
182- // Render the listing event
170+ // Render the event
183171 $ eventData = [
184172 'item_list_id ' => $ item_list_id ,
185173 'item_list_name ' => $ item_list_name ,
@@ -201,7 +189,7 @@ private function renderSearch()
201189 return ;
202190 }
203191
204- // Render the listing event
192+ // Render the event
205193 $ eventData = [
206194 'search_term ' => (string ) $ _GET ['s ' ],
207195 ];
@@ -213,8 +201,6 @@ private function renderSearch()
213201
214202 /**
215203 * This method renders tracking code for product listings, like category pages.
216- *
217- * @return string
218204 */
219205 private function renderCartpage ()
220206 {
@@ -231,16 +217,9 @@ private function renderCartpage()
231217
232218 // Prepare items to our format
233219 $ 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
220+ $ items = $ productWrapper ->prepareItemListFromProductList ($ cart ['products ' ], true );
221+
222+ // Render the event
244223 $ eventData = [
245224 'currency ' => $ this ->context ->currency ->iso_code ,
246225 'value ' => $ cart ['totals ' ]['total ' ]['amount ' ],
@@ -254,8 +233,6 @@ private function renderCartpage()
254233
255234 /**
256235 * This method renders tracking code for product listings, like category pages.
257- *
258- * @return string
259236 */
260237 private function renderBeginCheckout ()
261238 {
@@ -284,16 +261,9 @@ private function renderBeginCheckout()
284261
285262 // Prepare items to our format
286263 $ 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- }
264+ $ items = $ productWrapper ->prepareItemListFromProductList ($ cart ['products ' ], true );
295265
296- // Render the listing event
266+ // Render the event
297267 $ eventData = [
298268 'currency ' => $ this ->context ->currency ->iso_code ,
299269 'value ' => $ cart ['totals ' ]['total ' ]['amount ' ],
@@ -304,4 +274,53 @@ private function renderBeginCheckout()
304274 $ eventData
305275 );
306276 }
277+
278+ /**
279+ * This method renders tracking code after user logs in.
280+ */
281+ private function renderLogin ()
282+ {
283+ // Render it only on login page AND if we are not creating a new account in older PS versions
284+ // For newer versions, registrations are handled with standalone registration controller.
285+ if ($ this ->context ->controller ->php_self != 'authentication ' || isset ($ _GET ['create_account ' ])) {
286+ return ;
287+ }
288+
289+ // Render the event
290+ $ this ->gaScripts .= $ this ->module ->getTools ()->renderEvent ('login ' , []);
291+ }
292+
293+ /**
294+ * This method renders tracking code after user registers.
295+ */
296+ private function renderRegistration ()
297+ {
298+ if ($ this ->context ->controller ->php_self != 'registration ' &&
299+ ($ this ->context ->controller ->php_self != 'authentication ' || !isset ($ _GET ['create_account ' ]))
300+ ) {
301+ return ;
302+ }
303+
304+ // Render the event
305+ $ this ->gaScripts .= $ this ->module ->getTools ()->renderEvent ('sign_up ' , []);
306+ }
307+
308+ /**
309+ * Saves information about last visited product listing, so we can later use it for select_item event.
310+ */
311+ private function saveInformationAboutListing ()
312+ {
313+ // Try to get product list variable
314+ $ listing = $ this ->context ->smarty ->getTemplateVars ('listing ' );
315+ if (empty ($ listing ['products ' ]) || empty ($ listing ['label ' ])) {
316+ return ;
317+ }
318+
319+ // Save this information to a cookie
320+ $ this ->context ->cookie ->ga_last_listing = json_encode ([
321+ 'item_list_url ' => $ _SERVER ['REQUEST_URI ' ],
322+ 'item_list_id ' => $ this ->context ->controller ->php_self ,
323+ 'item_list_name ' => $ listing ['label ' ],
324+ ]);
325+ }
307326}
0 commit comments