@@ -98,13 +98,13 @@ public function enqueue_script() {
9898 echo $ this ->build_event ( 'PageView ' , array () );
9999 ?>
100100 </script>
101-
102- <noscript><img height="1" width="1" style="display:none"
103- src="https://www.facebook.com/tr?id=<?php echo $ facebook_pixel_id ; ?> &ev=PageView&noscript=1"
104- /></noscript>
105101 <?php
106102
107- $ this ->add_to_cart_ajax ();
103+ $ this ->print_event_script ();
104+
105+ if ( ! class_exists ( 'WeDevs_WC_Conversion_Tracking_Pro ' ) ) {
106+ $ this ->add_to_cart_ajax ();
107+ }
108108 }
109109
110110 /**
@@ -139,13 +139,17 @@ public function add_to_cart_ajax() {
139139 if ( ! $ this ->event_enabled ( 'AddToCart ' ) ) {
140140 return ;
141141 }
142+
143+ $ integration_settins = $ this ->get_integration_settings ();
144+ $ facebook_pixel_id = ! empty ( $ integration_settins [0 ]['pixel_id ' ] ) ? $ integration_settins [0 ]['pixel_id ' ] : '' ;
142145 ?>
143146 <script type="text/javascript">
144147 jQuery(function($) {
145- $(document).on('added_to_cart', function (event, fragments, hash, button) {
146- fbq('track', 'AddToCart', {
147- content_ids: [ $(button).data('product_id') ],
148- content_type: 'product',
148+ $(document).on('added_to_cart', function (event, fragments, dhash, button) {
149+ wcfbq('<?php echo $ facebook_pixel_id ?> ', 'AddToCart', {
150+ content_ids: [ $(button).data('product_id') ],
151+ content_type: 'product',
152+ currency: '<?php echo get_woocommerce_currency ()?> '
149153 });
150154 });
151155 });
@@ -247,6 +251,73 @@ public function get_product_categories( $product_id ) {
247251 'categories ' => $ categories
248252 );
249253 }
254+
255+ public function print_event_script () {
256+ ?>
257+ <script>
258+ (function (window, document) {
259+ if (window.wcfbq) return;
260+ window.wcfbq = (function () {
261+ if (arguments.length > 0) {
262+ var pixelId, trackType, contentObj;
263+
264+ if (typeof arguments[0] == 'string') pixelId = arguments[0];
265+ if (typeof arguments[1] == 'string') trackType = arguments[1];
266+ if (typeof arguments[2] == 'object') contentObj = arguments[2];
267+
268+ var params = [];
269+ if (typeof pixelId === 'string' && pixelId.replace(/\s+/gi, '') != '' &&
270+ typeof trackType === 'string' && trackType.replace(/\s+/gi, '')) {
271+ params.push('id=' + encodeURIComponent(pixelId));
272+ switch (trackType) {
273+ case 'PageView':
274+ case 'ViewContent':
275+ case 'Search':
276+ case 'AddToCart':
277+ case 'InitiateCheckout':
278+ case 'AddPaymentInfo':
279+ case 'Lead':
280+ case 'CompleteRegistration':
281+ case 'Purchase':
282+ case 'AddToWishlist':
283+ params.push('ev=' + encodeURIComponent(trackType));
284+ break;
285+ default:
286+ return;
287+ }
288+
289+ params.push('dl=' + encodeURIComponent(document.location.href));
290+ if (document.referrer) params.push('rl=' + encodeURIComponent(document.referrer));
291+ params.push('if=false');
292+ params.push('ts=' + new Date().getTime());
293+
294+ if (typeof contentObj == 'object') {
295+ for (var u in contentObj) {
296+ if (typeof contentObj[u] == 'object' && contentObj[u] instanceof Array) {
297+ if (contentObj[u].length > 0) {
298+ for (var y = 0; y < contentObj[u].length; y++) { contentObj[u][y] = (contentObj[u][y] + '').replace(/^\s+|\s+$/gi, '').replace(/\s+/gi, ' ').replace(/,/gi, '§'); }
299+ params.push('cd[' + u + ']=' + encodeURIComponent(contentObj[u].join(',').replace(/^/gi, '[\'').replace(/$/gi, '\']').replace(/,/gi, '\',\'').replace(/§/gi, '\,')));
300+ }
301+ }
302+ else if (typeof contentObj[u] == 'string')
303+ params.push('cd[' + u + ']=' + encodeURIComponent(contentObj[u]));
304+ }
305+ }
306+
307+ params.push('v=' + encodeURIComponent('2.7.19'));
308+
309+ var imgId = new Date().getTime();
310+ var img = document.createElement('img');
311+ img.id = 'fb_' + imgId, img.src = 'https://www.facebook.com/tr/?' + params.join('&'), img.width = 1, img.height = 1, img.style = 'display:none;';
312+ document.body.appendChild(img);
313+ window.setTimeout(function () { var t = document.getElementById('fb_' + imgId); t.parentElement.removeChild(t); }, 1000);
314+ }
315+ }
316+ });
317+ })(window, document);
318+ </script>
319+ <?php
320+ }
250321}
251322
252323return new WCCT_Integration_Facebook ();
0 commit comments