You are on page 1of 3

<!

-- Facebook Pixel Code -->

<script>

// Facebook Pixel Initialization

!function(f,b,e,v,n,t,s) {

if(f.fbq)return;n=f.fbq=function(){n.callMethod?

n.callMethod.apply(n,arguments):n.queue.push(arguments)};

if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';

n.queue=[];t=b.createElement(e);t.async=!0;

t.src=v;s=b.getElementsByTagName(e)[0];

s.parentNode.insertBefore(t,s)

}(window, document,'script','https://connect.facebook.net/en_US/fbevents.js');

fbq('init', '1296505841007521');

fbq('track', 'PageView');

</script>

<noscript>

<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?


id=1296505841007521&ev=PageView&noscript=1"/>

</noscript>

<!-- End Facebook Pixel Code -->

<!-- Google tag (gtag.js) -->

<script async src="https://www.googletagmanager.com/gtag/js?id=G-R7KN2H5RTQ"></script>

<script>

window.dataLayer = window.dataLayer || [];

function gtag(){dataLayer.push(arguments);}

gtag('js', new Date());

gtag('config', 'G-R7KN2H5RTQ');
</script>

Above code is for Header section

<!-- Event Tracking Functions -->


<script>
// Facebook Pixel and Google Analytics Event Tracking with Dynamic Event Names using
Product Name and Value
function track_add_to_cart(product_name, price) {
fbq('track', 'AddToCart - ' + product_name, {
content_name: product_name,
content_type: 'product',
value: price,
currency: 'INR', // Replace with your currency code
});
gtag('event', 'add_to_cart - ' + product_name, {
'event_category': 'Product',
'event_label': 'Product Name: ' + product_name,
'value': price,
'currency': 'INR' // Replace with your currency code
});
}

function track_initiate_checkout(product_name) {
fbq('track', 'InitiateCheckout - ' + product_name, {
content_name: product_name,
content_type: 'product',
});
gtag('event', 'begin_checkout - ' + product_name, {
'event_category': 'Product',
'event_label': 'Product Name: ' + product_name
});
}

function track_purchase(product_name, price) {


fbq('track', 'Purchase - ' + product_name, {
content_name: product_name,
content_type: 'product',
value: price,
currency: 'INR', // Replace with your currency code
});
gtag('event', 'purchase - ' + product_name, {
'event_category': 'Product',
'event_label': 'Product Name: ' + product_name,
'value': price,
'currency': 'INR' // Replace with your currency code
});
}
</script>
<!-- End Event Tracking Functions -->

Above code is for body

<!-- Example Usage on a Product Page -->


<?php
$product_name = get_the_title(); // Fetch the product name
$product_price = get_post_meta(get_the_ID(), '_price', true); // Fetch the product price

// Add to Cart Button


echo '<button onclick="track_add_to_cart(\'' . $product_name . '\', ' . $product_price . ')">Add to
Cart</button>';

// Initiate Checkout Button


echo '<button onclick="track_initiate_checkout(\'' . $product_name . '\')">Initiate
Checkout</button>';

// After Successful Purchase


echo '<script>track_purchase(\'' . $product_name . '\', ' . $product_price . ')</script>';
?>

Put this code on product pages

You might also like