Adding Facebook Pixel Goals to OpenCart 2x
If you need to manually add the Facebook Pixel code, the script itself is easily added to the head, but you need to somehow transfer the product parameters to the header, i.e. product data must first go to the header controller and from there we output them to the header template script.
So far, there is a solution only for the purpose of visiting pages and adding to the cart (the tracking code is identical there, except for one single variable).
View product page
fbq('track', 'ViewContent', {
content_type: 'product',
content_ids: ['1234'],
content_name: 'Pampers diapers 80pcs', content_category: 'Diapers',
value: 20.45,
currency: 'USD' });
Adding an item to the cart
fbq('track', 'AddToCart', {
content_type: 'product',
content_ids: ['1234'],
content_name: 'Pampers diapers 80pcs',
content_category: 'Diapers', value: 20.45,
currency: 'USD'
});
We see that the passed variables are identical.
First, let's pass the product and category data to the catalog / controller / common / header.php controller
if (isset($this->request->get['product_id'])) {
$product_id = $this->request->get['product_id'];
} else {
$product_id = 0;
}
$product_info = $this->model_catalog_product->getProduct($product_id);
$this->data['product_info'] = $product_info;
if ($product_info) {
$data['price'] = $product_info['price'];
$data['name'] = $product_info['name'];
$data['prod_id'] = $product_info['product_id'];
}
$data['catprod'] = array();
$product_category = $this->model_catalog_product->getCategories($product_info['product_id']);
foreach ($product_category as $prodcat) {
$category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
if ($category_info) {
$data['catprod'][] = array( 'name' => $category_info['name'] );
}
}
You need to insert after receiving the menu data, where we connect the models of categories and products
$this->load->model('catalog/category');
$this->load->model('catalog/product');
Now, in our template catalog / view / theme / default / template / common / header.tpl, you can display product data and product category in the script
fbq('track', 'ViewContent', {
content_type: 'product',
content_ids: ['<?php echo $prod_id; ?>'],
content_name: '<?php echo $name; ?>',
content_category: '<?php $numItems = count($catprod); $i = 0; foreach($catprod as $key=>$value) { if(++$i === $numItems) { echo $value['name']; } } ?>',
value: <?php $price = substr($price, 0, -2); echo $price; ?>,
currency: 'USD'
});
I will make a reservation right away that there is a crutch in the category - an array with an associated product category and a parent of this category is initially transmitted, and through a loop I output the last element of the array i.e. the attached katu itself, without a parent, and also, in the price - the value with 4 zeros after the dot is displayed. I think both of these crutches can be fixed at the controller data level, but I needed a solution "for yesterday" and did it as quickly as possible.
In addition to these two goals, it will probably be possible later to make a code for the purposes of starting an order and ending an order, until I can imagine how even this data should look in its raw form.
You can check the pages with the code using the browser add-on - Facebook Pixel Helper, which, after installation, will show in the browser whether there are pixel targets and possible errors.