2424
2525class GoogleAnalyticsTools
2626{
27- /**
28- * filter
29- *
30- * @param string $gaScripts
31- * @param int $filterable
32- *
33- * @return string
34- */
35- public function filter ($ gaScripts , $ filterable )
36- {
37- if (1 == $ filterable ) {
38- return implode ('; ' , array_unique (explode ('; ' , $ gaScripts )));
39- }
40-
41- return $ gaScripts ;
42- }
43-
4427 /**
4528 * Renders purchase event for order
4629 *
@@ -57,127 +40,30 @@ public function renderPurchaseEvent($orderProducts, $orderData, $callbackUrl)
5740 }
5841
5942 $ callbackData = [
60- 'orderid ' => $ orderData ['id ' ],
43+ 'orderid ' => $ orderData ['transaction_id ' ],
6144 'customer ' => $ orderData ['customer ' ],
6245 ];
6346
6447 $ eventData = [
65- 'transaction_id ' => (int ) $ orderData ['id ' ],
48+ 'transaction_id ' => (int ) $ orderData ['transaction_id ' ],
6649 'affiliation ' => $ orderData ['affiliation ' ],
67- 'value ' => (float ) $ orderData ['revenue ' ],
50+ 'value ' => (float ) $ orderData ['value ' ],
6851 'tax ' => (float ) $ orderData ['tax ' ],
6952 'shipping ' => (float ) $ orderData ['shipping ' ],
7053 'currency ' => $ orderData ['currency ' ],
71- 'items ' => [] ,
54+ 'items ' => $ orderProducts ,
7255 'event_callback ' => "function() {
7356 $.get(' " . $ callbackUrl . "', " . json_encode ($ callbackData , JSON_UNESCAPED_UNICODE ) . ');
7457 } ' ,
7558 ];
7659
77- foreach ($ orderProducts as $ product ) {
78- $ eventData ['items ' ][] = [
79- 'item_id ' => (int ) $ product ['id ' ],
80- 'item_name ' => $ product ['name ' ],
81- 'quantity ' => (int ) $ product ['quantity ' ],
82- 'price ' => (float ) $ product ['price ' ],
83- ];
84- }
85-
8660 return $ this ->renderEvent (
8761 'purchase ' ,
8862 $ eventData ,
8963 ['event_callback ' ]
9064 );
9165 }
9266
93- /**
94- * addProductClick
95- *
96- * @param array $products
97- * @param string $currencyIsoCode
98- *
99- * @return string|void
100- */
101- public function addProductClick ($ products , $ currencyIsoCode )
102- {
103- if (!is_array ($ products )) {
104- return ;
105- }
106-
107- $ js = '' ;
108- foreach ($ products as $ key => $ product ) {
109- $ eventData = [
110- 'items ' => [
111- 'item_id ' => (int ) $ product ['id ' ],
112- 'item_name ' => $ product ['name ' ],
113- 'quantity ' => (int ) $ product ['quantity ' ],
114- 'price ' => (float ) $ product ['price ' ],
115- 'currency ' => $ currencyIsoCode ,
116- 'index ' => (int ) $ product ['position ' ],
117- 'item_brand ' => $ product ['brand ' ],
118- 'item_category ' => $ product ['category ' ],
119- 'item_list_id ' => $ product ['list ' ],
120- 'item_variant ' => $ product ['variant ' ],
121- ],
122- ];
123-
124- // Add send_to parameter to avoid sending extra events
125- // to other gtag configs (Ads for example).
126- $ eventData = array_merge (
127- ['send_to ' => Configuration::get ('GA_ACCOUNT_ID ' )],
128- $ eventData
129- );
130-
131- $ productId = explode ('- ' , $ product ['id ' ]);
132- $ js .= '$( \'article[data-id-product=" ' . $ productId [0 ] . '"] a.quick-view \').on(
133- "click",
134- function() {
135- gtag("event", "select_item", ' . json_encode ($ eventData , JSON_UNESCAPED_UNICODE ) . ')
136- }); ' ;
137- }
138-
139- return $ js ;
140- }
141-
142- /**
143- * addProductClickByHttpReferal
144- *
145- * @param array $products
146- *
147- * @return string|void
148- */
149- public function addProductClickByHttpReferal ($ products , $ currencyIsoCode )
150- {
151- if (!is_array ($ products )) {
152- return ;
153- }
154-
155- $ js = '' ;
156- foreach ($ products as $ key => $ product ) {
157- $ eventData = [
158- 'items ' => [
159- 'item_id ' => (int ) $ product ['id ' ],
160- 'item_name ' => $ product ['name ' ],
161- 'quantity ' => (int ) $ product ['quantity ' ],
162- 'price ' => (float ) $ product ['price ' ],
163- 'currency ' => $ currencyIsoCode ,
164- 'index ' => (int ) $ product ['position ' ],
165- 'item_brand ' => $ product ['brand ' ],
166- 'item_category ' => $ product ['category ' ],
167- 'item_list_id ' => $ product ['list ' ],
168- 'item_variant ' => $ product ['variant ' ],
169- ],
170- ];
171-
172- $ js .= $ this ->renderEvent (
173- 'select_item ' ,
174- $ eventData
175- );
176- }
177-
178- return $ js ;
179- }
180-
18167 /**
18268 * Encodes array of data into JSON, optionally ignoring some of the values
18369 *
0 commit comments