2424
2525class GoogleAnalyticsTools
2626{
27- /**
28- * @var bool
29- */
30- protected $ isV4Enabled = false ;
31-
32- public function __construct ($ isV4Enabled = false )
33- {
34- $ this ->isV4Enabled = $ isV4Enabled ;
35- }
36-
3727 /**
3828 * filter
3929 *
@@ -65,70 +55,38 @@ public function addTransaction($products, $transaction)
6555 return ;
6656 }
6757
68- if ($ this ->isV4Enabled ) {
69- $ callbackData = [
70- 'orderid ' => $ transaction ['id ' ],
71- 'customer ' => $ transaction ['customer ' ],
72- ];
73-
74- $ eventData = [
75- 'transaction_id ' => (int ) $ transaction ['id ' ],
76- 'affiliation ' => $ transaction ['affiliation ' ],
77- 'value ' => (float ) $ transaction ['revenue ' ],
78- 'tax ' => (float ) $ transaction ['tax ' ],
79- 'shipping ' => (float ) $ transaction ['shipping ' ],
80- 'currency ' => $ transaction ['currency ' ],
81- 'items ' => [],
82- 'event_callback ' => "function() {
83- $.get(' " . $ transaction ['url ' ] . "', " . json_encode ($ callbackData , JSON_UNESCAPED_UNICODE ) . ');
84- } ' ,
58+ $ callbackData = [
59+ 'orderid ' => $ transaction ['id ' ],
60+ 'customer ' => $ transaction ['customer ' ],
61+ ];
62+
63+ $ eventData = [
64+ 'transaction_id ' => (int ) $ transaction ['id ' ],
65+ 'affiliation ' => $ transaction ['affiliation ' ],
66+ 'value ' => (float ) $ transaction ['revenue ' ],
67+ 'tax ' => (float ) $ transaction ['tax ' ],
68+ 'shipping ' => (float ) $ transaction ['shipping ' ],
69+ 'currency ' => $ transaction ['currency ' ],
70+ 'items ' => [],
71+ 'event_callback ' => "function() {
72+ $.get(' " . $ transaction ['url ' ] . "', " . json_encode ($ callbackData , JSON_UNESCAPED_UNICODE ) . ');
73+ } ' ,
74+ ];
75+
76+ foreach ($ products as $ product ) {
77+ $ eventData ['items ' ][] = [
78+ 'item_id ' => (int ) $ product ['id ' ],
79+ 'item_name ' => $ product ['name ' ],
80+ 'quantity ' => (int ) $ product ['quantity ' ],
81+ 'price ' => (float ) $ product ['price ' ],
8582 ];
86-
87- foreach ($ products as $ product ) {
88- $ eventData ['items ' ][] = [
89- 'item_id ' => (int ) $ product ['id ' ],
90- 'item_name ' => $ product ['name ' ],
91- 'quantity ' => (int ) $ product ['quantity ' ],
92- 'price ' => (float ) $ product ['price ' ],
93- ];
94- }
95-
96- $ js = $ this ->renderEvent (
97- 'purchase ' ,
98- $ eventData ,
99- ['event_callback ' ]
100- );
101- } else {
102- unset($ transaction ['currency ' ]);
103- $ js = '' ;
104- foreach ($ products as $ product ) {
105- $ js .= 'MBG.add( ' . json_encode ($ product ) . '); ' ;
106- }
107- $ js .= 'MBG.addTransaction( ' . json_encode ($ transaction ) . '); ' ;
10883 }
10984
110- return $ js ;
111- }
112-
113- /**
114- * add product impression js and product click js
115- *
116- * @param array $products
117- *
118- * @return string|void
119- */
120- public function addProductImpression ($ products )
121- {
122- if (!is_array ($ products )) {
123- return ;
124- }
125-
126- $ js = '' ;
127- if (!$ this ->isV4Enabled ) {
128- foreach ($ products as $ product ) {
129- $ js .= 'MBG.add( ' . json_encode ($ product ) . ",'',true); " ;
130- }
131- }
85+ $ js = $ this ->renderEvent (
86+ 'purchase ' ,
87+ $ eventData ,
88+ ['event_callback ' ]
89+ );
13290
13391 return $ js ;
13492 }
@@ -148,41 +106,35 @@ public function addProductClick($products, $currencyIsoCode)
148106 }
149107
150108 $ js = '' ;
151- if ($ this ->isV4Enabled ) {
152- foreach ($ products as $ key => $ product ) {
153- $ eventData = [
154- 'items ' => [
155- 'item_id ' => (int ) $ product ['id ' ],
156- 'item_name ' => $ product ['name ' ],
157- 'quantity ' => (int ) $ product ['quantity ' ],
158- 'price ' => (float ) $ product ['price ' ],
159- 'currency ' => $ currencyIsoCode ,
160- 'index ' => (int ) $ product ['position ' ],
161- 'item_brand ' => $ product ['brand ' ],
162- 'item_category ' => $ product ['category ' ],
163- 'item_list_id ' => $ product ['list ' ],
164- 'item_variant ' => $ product ['variant ' ],
165- ],
166- ];
109+ foreach ($ products as $ key => $ product ) {
110+ $ eventData = [
111+ 'items ' => [
112+ 'item_id ' => (int ) $ product ['id ' ],
113+ 'item_name ' => $ product ['name ' ],
114+ 'quantity ' => (int ) $ product ['quantity ' ],
115+ 'price ' => (float ) $ product ['price ' ],
116+ 'currency ' => $ currencyIsoCode ,
117+ 'index ' => (int ) $ product ['position ' ],
118+ 'item_brand ' => $ product ['brand ' ],
119+ 'item_category ' => $ product ['category ' ],
120+ 'item_list_id ' => $ product ['list ' ],
121+ 'item_variant ' => $ product ['variant ' ],
122+ ],
123+ ];
167124
168- // Add send_to parameter to avoid sending extra events
169- // to other gtag configs (Ads for example).
170- $ eventData = array_merge (
171- ['send_to ' => Configuration::get ('GA_ACCOUNT_ID ' )],
172- $ eventData
173- );
125+ // Add send_to parameter to avoid sending extra events
126+ // to other gtag configs (Ads for example).
127+ $ eventData = array_merge (
128+ ['send_to ' => Configuration::get ('GA_ACCOUNT_ID ' )],
129+ $ eventData
130+ );
174131
175- $ productId = explode ('- ' , $ product ['id ' ]);
176- $ js .= '$( \'article[data-id-product=" ' . $ productId [0 ] . '"] a.quick-view \').on(
177- "click",
178- function() {
179- gtag("event", "select_item", ' . json_encode ($ eventData , JSON_UNESCAPED_UNICODE ) . ')
180- }); ' ;
181- }
182- } else {
183- foreach ($ products as $ product ) {
184- $ js .= 'MBG.addProductClick( ' . json_encode ($ product ) . '); ' ;
185- }
132+ $ productId = explode ('- ' , $ product ['id ' ]);
133+ $ js .= '$( \'article[data-id-product=" ' . $ productId [0 ] . '"] a.quick-view \').on(
134+ "click",
135+ function() {
136+ gtag("event", "select_item", ' . json_encode ($ eventData , JSON_UNESCAPED_UNICODE ) . ')
137+ }); ' ;
186138 }
187139
188140 return $ js ;
@@ -202,55 +154,26 @@ public function addProductClickByHttpReferal($products, $currencyIsoCode)
202154 }
203155
204156 $ js = '' ;
205- if ($ this ->isV4Enabled ) {
206- foreach ($ products as $ key => $ product ) {
207- $ eventData = [
208- 'items ' => [
209- 'item_id ' => (int ) $ product ['id ' ],
210- 'item_name ' => $ product ['name ' ],
211- 'quantity ' => (int ) $ product ['quantity ' ],
212- 'price ' => (float ) $ product ['price ' ],
213- 'currency ' => $ currencyIsoCode ,
214- 'index ' => (int ) $ product ['position ' ],
215- 'item_brand ' => $ product ['brand ' ],
216- 'item_category ' => $ product ['category ' ],
217- 'item_list_id ' => $ product ['list ' ],
218- 'item_variant ' => $ product ['variant ' ],
219- ],
220- ];
221-
222- $ js .= $ this ->renderEvent (
223- 'select_item ' ,
224- $ eventData
225- );
226- }
227- } else {
228- foreach ($ products as $ product ) {
229- $ js .= 'MBG.addProductClickByHttpReferal( ' . json_encode ($ product ) . '); ' ;
230- }
231- }
232-
233- return $ js ;
234- }
235-
236- /**
237- * Add product checkout info
238- *
239- * @param array $products
240- *
241- * @return string|void
242- */
243- public function addProductFromCheckout ($ products )
244- {
245- if (!is_array ($ products )) {
246- return ;
247- }
157+ foreach ($ products as $ key => $ product ) {
158+ $ eventData = [
159+ 'items ' => [
160+ 'item_id ' => (int ) $ product ['id ' ],
161+ 'item_name ' => $ product ['name ' ],
162+ 'quantity ' => (int ) $ product ['quantity ' ],
163+ 'price ' => (float ) $ product ['price ' ],
164+ 'currency ' => $ currencyIsoCode ,
165+ 'index ' => (int ) $ product ['position ' ],
166+ 'item_brand ' => $ product ['brand ' ],
167+ 'item_category ' => $ product ['category ' ],
168+ 'item_list_id ' => $ product ['list ' ],
169+ 'item_variant ' => $ product ['variant ' ],
170+ ],
171+ ];
248172
249- $ js = '' ;
250- if (!$ this ->isV4Enabled ) {
251- foreach ($ products as $ product ) {
252- $ js .= 'MBG.add( ' . json_encode ($ product ) . '); ' ;
253- }
173+ $ js .= $ this ->renderEvent (
174+ 'select_item ' ,
175+ $ eventData
176+ );
254177 }
255178
256179 return $ js ;
0 commit comments