Skip to content

Commit 8d2d09f

Browse files
committed
Fix ajax add to cart event
1 parent 02f1b85 commit 8d2d09f

4 files changed

Lines changed: 1190 additions & 936 deletions

File tree

includes/integrations/class-integration-facebook.php

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

252323
return new WCCT_Integration_Facebook();

languages/woocommerce-conversion-tracking.pot

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
# Copyright (C) 2018 Tareq Hasan
1+
# Copyright (C) 2019 Tareq Hasan
22
# This file is distributed under the GPL2.
33
msgid ""
44
msgstr ""
5-
"Project-Id-Version: WooCommerce Conversion Tracking 2.0\n"
5+
"Project-Id-Version: WooCommerce Conversion Tracking 2.0.2\n"
66
"Report-Msgid-Bugs-To: https://example.com\n"
7-
"POT-Creation-Date: 2018-02-22 11:39:03+00:00\n"
7+
"POT-Creation-Date: 2019-04-29 05:55:29+00:00\n"
88
"MIME-Version: 1.0\n"
99
"Content-Type: text/plain; charset=utf-8\n"
1010
"Content-Transfer-Encoding: 8bit\n"
11-
"PO-Revision-Date: 2018-MO-DA HO:MI+ZONE\n"
11+
"PO-Revision-Date: 2019-MO-DA HO:MI+ZONE\n"
1212
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1313
"Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"
1414
"X-Generator: grunt-wp-i18n 0.5.4\n"
1515

16-
#: conversion-tracking.php:268
16+
#: conversion-tracking.php:267
1717
msgid "Get PRO"
1818
msgstr ""
1919

20-
#: conversion-tracking.php:271
20+
#: conversion-tracking.php:270
2121
msgid "Docs"
2222
msgstr ""
2323

24-
#: conversion-tracking.php:272
24+
#: conversion-tracking.php:271
2525
msgid "Settings"
2626
msgstr ""
2727

28+
#: conversion-tracking.php:284
29+
msgid ""
30+
"<b>Woocommerce conversion tracking</b> requires <a target=\"_blank\" "
31+
"href=\"https://wordpress.org/plugins/woocommerce/\">Woocommerce</a>"
32+
msgstr ""
33+
2834
#: includes/class-admin.php:49
2935
msgid "Conversion Tracking"
3036
msgstr ""
@@ -75,7 +81,7 @@ msgstr ""
7581
msgid "Premium Features"
7682
msgstr ""
7783

78-
#: includes/class-integration-pro-features.php:137
84+
#: includes/class-integration-pro-features.php:138
7985
msgid "Get Premium"
8086
msgstr ""
8187

@@ -185,7 +191,7 @@ msgid "Insert conversion tracking code for this product."
185191
msgstr ""
186192

187193
#: includes/integrations/class-integration-custom.php:13
188-
#: includes/views/settings.php:154
194+
#: includes/views/settings.php:155
189195
msgid "Custom"
190196
msgstr ""
191197

@@ -279,6 +285,10 @@ msgstr ""
279285
msgid "Perfect Audience"
280286
msgstr ""
281287

288+
#: includes/views/settings.php:154
289+
msgid "Bing Ads"
290+
msgstr ""
291+
282292
#. Plugin Name of the plugin/theme
283293
msgid "WooCommerce Conversion Tracking"
284294
msgstr ""

0 commit comments

Comments
 (0)