Facebook Pixel integration for Opencart
This Opencart extension will allow you to integrate the Facebook Pixel in your Opencart webshop(s), to track the revenue gained from Facebook ad campaigns. This extension will track the default events: ViewContent, AddToCart, Purchase, Search, AddToWishlist, InitiateCheckout, CompleteRegistration.
The Facebook pixel can be used for three main functions: 1) Building Custom Audiences from your website for remarketing; 2) Optimizing ads for conversions; 3) Tracking conversions and attributing them back to your ads.
Facebook Help Center
The conversion events will be reported to Facebook with parameters 'value', 'currency', 'content_ids' (= product ID), 'content_name' (= product name), 'num_items' (for the Purchase and InitiateCheckout events).
Installing the Facebook Pixel integration
Installation of this extension is very simple. There are two ways you can install the module.
Download the 'vQmod' version from the Opencart Extension platform. Next, make sure you have vQmod v2.5.1 installed.
Upload the 'fb_conversionpixel.xml' file to the /vqmod/xml/ folder on your webhost.
Done!
Download the .ocmod.xml file from the Opencart Extension platform and upload it with the built-in Extension Installer. Next, navigate to Extensions >> Modifications and refresh the modifications cache by clicking the button. Done!
By default, the database field where Opencart stores the modification files is limited in size. This extension (from v2.0.7) is too big to be stored in the database field, and will be cropped. The cropping will result in failure to refresh the modification cache with PHP warning:
PHP Warning: DOMDocument::loadXML(): CData section not finished <\script type="text/javascript">$ in Entity, line: 1413 in /Applications/MAMP/htdocs/oc_2020/admin/controller/extension/modification.php on line 139
2016-01-19 18:44:44 - PHP Warning: DOMDocument::loadXML(): Premature end of data in tag add line 1352 in Entity, line: 1413 in /Applications/MAMP/htdocs/oc_2020/admin/controller/extension/modification.php on line 139
Remove the extension from the modification list and refresh the modification cache to fully remove the OCMOD extension from your system. Now copy the fb_conversion_pixel.ocmod.xml file in root of the /system folder, then refresh the modification cache. This will install the OCMOD extension but this method doesn't have the file size limitation.
To store bigger modification files, you need to edit the database table "oc_modifications" -> change the 'xml' field type from 'TEXT' to 'MEDIUMTEXT'. Or you can run this SQL command in PHPMyAdmin:
ALTER TABLE `oc_modification` CHANGE `xml` `xml`MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;
(change the database prefix according to your DB_PREFIX).If you don't do this, the OCMOD extension will not work and refreshing the modification cache will fail consistently. Using the vQmod version doesn't require any workarounds.
To enable 'AddToCart' and 'AddToWishlist' events you have to manually edit the common.js file. (Both vQmod and OCMOD don't modify .js files)
From the root of your installation, navigate to the catalog > view > javascript folder and open the common.js file in a plain text-editor (eg. Notepad).
Depending on the version of Opencart you are using, modify the file accordingly (create a backup copy of the original file first!)
$('#cart-total').html(json['total']);
if(json['product_id']) {
fbq(['track', 'AddToCart', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
}
$('#wishlist-total').html(json['total']);
if(json['product_id']) {
fbq(['track', 'AddToWishlist', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
}
$('html, body').animate({ scrollTop: 0 }, 'slow');
if(json['product_id']) {
fbq(['track', 'AddToCart', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
}
$('html, body').animate({ scrollTop: 0 }, 'slow');
if(json['product_id']) {
fbq(['track', 'AddToWishlist', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
}
From the root of your installation, navigate to the catalog > view > theme > journal2 > js folder and open the journal.js file in a plain text-editor (eg. Notepad).
$('#cart-total').html(json['total']);
if(json['product_id']) {
fbq(['track', 'AddToCart', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
}
$('.wishlist-total .product-count').html(json['total'].match(/\(\d+\)/g)[0].replace('(', '').replace(')', ''));
if(json['product_id']) {
fbq(['track', 'AddToWishlist', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
}
$('#cart ul').load('index.php?route=common/cart/info ul li');
if(json['product_id']) {
fbq(['track', 'AddToCart', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
}
$('.wishlist-total .product-count').html(json['total'].match(/\(\d+\)/g)[0].replace('(', '').replace(')', ''));
if(json['product_id']) {
fbq(['track', 'AddToWishlist', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
}
Facebook Pixel Implementation Guide
Navigate to Facebook Help Center - Facebook Implementation Guide and read the documentation.
The Facebook Pixel ID is the same id as its predecessor, the Custom Audience Pixel. In case you haven't created a Custom Audience Pixel previously, follow these guidelines to create a Facebook Pixel ID:
In the backend of your webshop navigate to System/Settings and click the 'Edit' button next to the webshop where you want to implement a Facebook Pixel.
Under the 'Facebook Pixel(s)' tab, paste the Facebook Pixel ID in the designated input field.
To learn how to fully benefit from this Facebook feature visit the Facebook Help Center - Audiences section
Facebook Product Catalog
To run Dynamic Product Ads (DPA) campaigns, you will have to upload your product catalog to the Facebook Business Manager.
Configuring Facebook Conversion Pixels
Navigate to the Facebook Ads Manager and select 'Conversion Tracking'.
Click on the Create Pixel button in the upper right corner of the screen, a popup window will appear.
Choose an appropriate category from the dropdown and name your pixel. It is good practice to include the webshop url/name and campaign name in the pixel name eg. Yourwebshop.url - Campaign
Confirm by clicking 'Create Pixel'.
Next, a popup window 'View Pixel Code' will appear. You can close this window and return to the Conversion Tracking pixel overview.
In the backend of your webshop navigate to System/Settings and click the 'Edit' button next to the webshop where you want to integrate your conversion pixel. Under the 'Facebook Pixel(s)' tab create a pixel by clicking on 'Add Conversion Pixel'.
Paste in the pixel ID and select the appropriate Conversion Pixel Category for the pixel:
Click Save when you are finished configuring the pixel(s).
For easy troubleshooting it is recommended to use the Facebook Pixel Helper (for Chrome).
No support is provided for this extension.