@@ -64,25 +64,40 @@ public function addTransaction($products, $transaction)
6464 }
6565
6666 if ($ this ->isV4Enabled ) {
67- $ js = 'gtag( \'event \', \'purchase \', {
68- "transaction_id": " ' . $ transaction ['id ' ] . '",
69- "items": [ ' ;
67+ $ callbackData = [
68+ 'orderid ' => $ transaction ['id ' ],
69+ 'customer ' => $ transaction ['customer ' ],
70+ ];
71+
72+ $ eventData = [
73+ 'transaction_id ' => (int ) $ transaction ['id ' ],
74+ 'affiliation ' => $ transaction ['affiliation ' ],
75+ 'value ' => (float ) $ transaction ['revenue ' ],
76+ 'tax ' => (float ) $ transaction ['tax ' ],
77+ 'shipping ' => (float ) $ transaction ['shipping ' ],
78+ 'currency ' => $ transaction ['currency ' ],
79+ 'items ' => [],
80+ 'event_callback ' => "function() {
81+ $.get(' " . $ transaction ['url ' ] . "', " . json_encode ($ callbackData , JSON_UNESCAPED_UNICODE ) . ');
82+ } ' ,
83+ ];
7084
71- $ isFirst = true ;
7285 foreach ($ products as $ product ) {
73- if (!$ isFirst ) {
74- $ js .= ', ' ;
75- }
76- $ js .= '{
77- "item_id": " ' . $ product ['id ' ] . '",
78- "item_name": " ' . $ product ['name ' ] . '",
79- "quantity": " ' . $ product ['quantity ' ] . '",
80- "price": " ' . $ product ['price ' ] . '"
81- } ' ;
82- $ isFirst = false ;
86+ $ eventData ['items ' ][] = [
87+ 'item_id ' => (int ) $ product ['id ' ],
88+ 'item_name ' => $ product ['name ' ],
89+ 'quantity ' => (int ) $ product ['quantity ' ],
90+ 'price ' => (float ) $ product ['price ' ],
91+ ];
8392 }
84- $ js .= ']}); ' ;
93+
94+ $ js = $ this ->renderEvent (
95+ 'purchase ' ,
96+ $ eventData ,
97+ ['event_callback ' ]
98+ );
8599 } else {
100+ unset($ transaction ['currency ' ]);
86101 $ js = '' ;
87102 foreach ($ products as $ product ) {
88103 $ js .= 'MBG.add( ' . json_encode ($ product ) . '); ' ;
@@ -133,26 +148,26 @@ public function addProductClick($products, $currencyIsoCode)
133148 $ js = '' ;
134149 if ($ this ->isV4Enabled ) {
135150 foreach ($ products as $ key => $ product ) {
151+ $ eventData = [
152+ 'items ' => [
153+ 'item_id ' => (int ) $ product ['id ' ],
154+ 'item_name ' => $ product ['name ' ],
155+ 'quantity ' => (int ) $ product ['quantity ' ],
156+ 'price ' => (float ) $ product ['price ' ],
157+ 'currency ' => $ currencyIsoCode ,
158+ 'index ' => (int ) $ product ['position ' ],
159+ 'item_brand ' => $ product ['brand ' ],
160+ 'item_category ' => $ product ['category ' ],
161+ 'item_list_id ' => $ product ['list ' ],
162+ 'item_variant ' => $ product ['variant ' ],
163+ ],
164+ ];
165+
136166 $ productId = explode ('- ' , $ product ['id ' ]);
137167 $ js .= '$( \'article[data-id-product=" ' . $ productId [0 ] . '"] a.quick-view \').on(
138168 "click",
139169 function() {
140- gtag("event", "select_item", {
141- items: [
142- {
143- item_id: " ' . $ product ['id ' ] . '",
144- item_name: " ' . $ product ['name ' ] . '",
145- quantity: " ' . $ product ['quantity ' ] . '",
146- price: " ' . $ product ['price ' ] . '",
147- currency: " ' . $ currencyIsoCode . '",
148- index: ' . $ product ['position ' ] . ',
149- item_brand: " ' . $ product ['brand ' ] . '",
150- item_category: " ' . $ product ['category ' ] . '",
151- item_list_id: " ' . $ product ['list ' ] . '",
152- item_variant: " ' . $ product ['variant ' ] . '",
153- }
154- ]
155- })
170+ gtag("event", "select_item", ' . json_encode ($ eventData , JSON_UNESCAPED_UNICODE ) . ')
156171 }); ' ;
157172 }
158173 } else {
@@ -180,22 +195,25 @@ public function addProductClickByHttpReferal($products, $currencyIsoCode)
180195 $ js = '' ;
181196 if ($ this ->isV4Enabled ) {
182197 foreach ($ products as $ key => $ product ) {
183- $ js .= 'gtag("event", "select_item", {
184- items: [
185- {
186- item_id: " ' . $ product ['id ' ] . '",
187- item_name: " ' . $ product ['name ' ] . '",
188- quantity: " ' . $ product ['quantity ' ] . '",
189- price: " ' . $ product ['price ' ] . '",
190- currency: " ' . $ currencyIsoCode . '",
191- index: ' . $ product ['position ' ] . ',
192- item_brand: " ' . $ product ['brand ' ] . '",
193- item_category: " ' . $ product ['category ' ] . '",
194- item_list_id: " ' . $ product ['list ' ] . '",
195- item_variant: " ' . $ product ['variant ' ] . '",
196- }
197- ]
198- }); ' ;
198+ $ eventData = [
199+ 'items ' => [
200+ 'item_id ' => (int ) $ product ['id ' ],
201+ 'item_name ' => $ product ['name ' ],
202+ 'quantity ' => (int ) $ product ['quantity ' ],
203+ 'price ' => (float ) $ product ['price ' ],
204+ 'currency ' => $ currencyIsoCode ,
205+ 'index ' => (int ) $ product ['position ' ],
206+ 'item_brand ' => $ product ['brand ' ],
207+ 'item_category ' => $ product ['category ' ],
208+ 'item_list_id ' => $ product ['list ' ],
209+ 'item_variant ' => $ product ['variant ' ],
210+ ],
211+ ];
212+
213+ $ js .= $ this ->renderEvent (
214+ 'select_item ' ,
215+ $ eventData
216+ );
199217 }
200218 } else {
201219 foreach ($ products as $ product ) {
@@ -228,4 +246,46 @@ public function addProductFromCheckout($products)
228246
229247 return $ js ;
230248 }
249+
250+ /**
251+ * Encodes array of data into JSON, optionally ignoring some of the values
252+ *
253+ * @param array $data Data pairs
254+ * @param array $ignoredKeys Values of these keys won't be encoded, for literal output of functions
255+ *
256+ * @return string json encoded data
257+ */
258+ public function jsonEncodeWithBlacklist ($ data , $ ignoredKeys = [])
259+ {
260+ $ return = [];
261+
262+ foreach ($ data as $ k => $ v ) {
263+ if (in_array ($ k , $ ignoredKeys )) {
264+ $ return [] = json_encode ($ k , JSON_UNESCAPED_UNICODE ) . ': ' . $ v ;
265+ } else {
266+ $ return [] = json_encode ($ k , JSON_UNESCAPED_UNICODE ) . ': ' . json_encode ($ v , JSON_UNESCAPED_UNICODE );
267+ }
268+ }
269+
270+ return '{ ' . implode (', ' , $ return ) . '} ' ;
271+ }
272+
273+ /**
274+ * Renders gtag event and encodes the data. You can optionally pass which data keys you want to
275+ * output in a raw way - callbacks for example.
276+ *
277+ * @param string $eventName
278+ * @param array $eventData
279+ * @param array $ignoredKeys Values of these keys won't be encoded, for literal output of functions
280+ *
281+ * @return string render gtag event for output
282+ */
283+ public function renderEvent ($ eventName , $ eventData , $ ignoredKeys = [])
284+ {
285+ return sprintf (
286+ 'gtag("event", "%1$s", %2$s); ' ,
287+ $ eventName ,
288+ $ this ->jsonEncodeWithBlacklist ($ eventData , $ ignoredKeys )
289+ );
290+ }
231291}
0 commit comments