2222
2323class GoogleAnalyticsTools
2424{
25+ /**
26+ * @var bool
27+ */
28+ protected $ isV4Enabled = false ;
29+
30+ public function __construct ($ isV4Enabled = false )
31+ {
32+ $ this ->isV4Enabled = $ isV4Enabled ;
33+ }
34+
2535 /**
2636 * filter
2737 *
@@ -43,22 +53,44 @@ public function filter($gaScripts, $filterable)
4353 * add order transaction
4454 *
4555 * @param array $products
46- * @param array $order
56+ * @param array $transaction
4757 *
4858 * @return string|void
4959 */
50- public function addTransaction ($ products , $ order )
60+ public function addTransaction ($ products , $ transaction )
5161 {
5262 if (!is_array ($ products )) {
5363 return ;
5464 }
5565
56- $ js = '' ;
57- foreach ($ products as $ product ) {
58- $ js .= 'MBG.add( ' . json_encode ($ product ) . '); ' ;
66+ if ($ this ->isV4Enabled ) {
67+ $ js = 'gtag( \'event \', \'purchase \', {
68+ "transaction_id": " ' . $ transaction ['id ' ] . '",
69+ "items": [ ' ;
70+
71+ $ isFirst = true ;
72+ 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 ;
83+ }
84+ $ js .= ']}); ' ;
85+ } else {
86+ $ js = '' ;
87+ foreach ($ products as $ product ) {
88+ $ js .= 'MBG.add( ' . json_encode ($ product ) . '); ' ;
89+ }
90+ $ js .= 'MBG.addTransaction( ' . json_encode ($ transaction ) . '); ' ;
5991 }
6092
61- return $ js . ' MBG.addTransaction( ' . json_encode ( $ order ) . ' ); ' ;
93+ return $ js ;
6294 }
6395
6496 /**
@@ -75,8 +107,10 @@ public function addProductImpression($products)
75107 }
76108
77109 $ js = '' ;
78- foreach ($ products as $ product ) {
79- $ js .= 'MBG.add( ' . json_encode ($ product ) . ",'',true); " ;
110+ if (!$ this ->isV4Enabled ) {
111+ foreach ($ products as $ product ) {
112+ $ js .= 'MBG.add( ' . json_encode ($ product ) . ",'',true); " ;
113+ }
80114 }
81115
82116 return $ js ;
@@ -86,18 +120,45 @@ public function addProductImpression($products)
86120 * addProductClick
87121 *
88122 * @param array $products
123+ * @param string $currencyIsoCode
89124 *
90125 * @return string|void
91126 */
92- public function addProductClick ($ products )
127+ public function addProductClick ($ products, $ currencyIsoCode )
93128 {
94129 if (!is_array ($ products )) {
95130 return ;
96131 }
97132
98133 $ js = '' ;
99- foreach ($ products as $ product ) {
100- $ js .= 'MBG.addProductClick( ' . json_encode ($ product ) . '); ' ;
134+ if ($ this ->isV4Enabled ) {
135+ foreach ($ products as $ key => $ product ) {
136+ $ productId = explode ('- ' , $ product ['id ' ]);
137+ $ js .= '$( \'article[data-id-product=" ' . $ productId [0 ] . '"] a.quick-view \').on(
138+ "click",
139+ 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+ })
156+ }); ' ;
157+ }
158+ } else {
159+ foreach ($ products as $ product ) {
160+ $ js .= 'MBG.addProductClick( ' . json_encode ($ product ) . '); ' ;
161+ }
101162 }
102163
103164 return $ js ;
@@ -110,15 +171,36 @@ public function addProductClick($products)
110171 *
111172 * @return string|void
112173 */
113- public function addProductClickByHttpReferal ($ products )
174+ public function addProductClickByHttpReferal ($ products, $ currencyIsoCode )
114175 {
115176 if (!is_array ($ products )) {
116177 return ;
117178 }
118179
119180 $ js = '' ;
120- foreach ($ products as $ product ) {
121- $ js .= 'MBG.addProductClickByHttpReferal( ' . json_encode ($ product ) . '); ' ;
181+ if ($ this ->isV4Enabled ) {
182+ 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+ }); ' ;
199+ }
200+ } else {
201+ foreach ($ products as $ product ) {
202+ $ js .= 'MBG.addProductClickByHttpReferal( ' . json_encode ($ product ) . '); ' ;
203+ }
122204 }
123205
124206 return $ js ;
@@ -138,8 +220,10 @@ public function addProductFromCheckout($products)
138220 }
139221
140222 $ js = '' ;
141- foreach ($ products as $ product ) {
142- $ js .= 'MBG.add( ' . json_encode ($ product ) . '); ' ;
223+ if (!$ this ->isV4Enabled ) {
224+ foreach ($ products as $ product ) {
225+ $ js .= 'MBG.add( ' . json_encode ($ product ) . '); ' ;
226+ }
143227 }
144228
145229 return $ js ;
0 commit comments