0% found this document useful (0 votes)
65 views4 pages

Pixel Code Meowwlicious

The document provides code to track e-commerce events like add to cart, purchase, and checkout using Facebook pixel and Google Analytics tags. It includes code snippets to add to the header and body of home and product pages to initialize tracking and fire events for specific product actions. Functions are defined to track add to cart, initiate checkout, and purchase events, passing the product name and price as parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Pixel Code Meowwlicious

The document provides code to track e-commerce events like add to cart, purchase, and checkout using Facebook pixel and Google Analytics tags. It includes code snippets to add to the header and body of home and product pages to initialize tracking and fire events for specific product actions. Functions are defined to track add to cart, initiate checkout, and purchase events, passing the product name and price as parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<!

-- Loop through the products -->

<?php foreach ($products as $product) : ?>

<?php

// Fetch product details for each product in the loop

$product = wc_get_product($product->get_id()); // Get the WooCommerce product object

$product_name = $product->get_name(); // Get product name

$product_price = $product->get_price(); // Get product price

?>

<!-- Add to Cart Button -->

<button class="single_add_to_cart_button button alt" onclick="track_add_to_cart('<?php


echo esc_js($product_name); ?>', <?php echo esc_js($product_price); ?>)">Add to
Cart</button>

<?php endforeach; ?>

Paste this in head of home page


—-----------------------------------------------------------------------

<?php

global $product;

$product = wc_get_product(get_the_ID()); // Get the WooCommerce product object

$product_name = $product->get_name(); // Get product name


$product_price = $product->get_price(); // Get product price
?>

<!-- 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>

Paste this in head of individual product pages


—--------------------------------------------------------------------------------------------
<!-- Event Tracking Functions -->
<script>
function track_add_to_cart(product_name, price) {
fbq('trackCustom', '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('trackCustom', '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('trackCustom', '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 -->

<!-- Add to Cart Button -->


<button class="single_add_to_cart_button button alt" onclick="track_add_to_cart('<?php echo
esc_js($product_name); ?>', <?php echo esc_js($product_price); ?>)">Add to Cart</button>

<!-- Initiate Checkout Button -->


<a href="<?php echo esc_url(wc_get_checkout_url()); ?>" class="checkout-button button alt"
onclick="track_initiate_checkout('<?php echo esc_js($product_name); ?>')">Initiate
Checkout</a>

<!-- After Successful Purchase -->


<script>
track_purchase('<?php echo esc_js($product_name); ?>', <?php echo
esc_js($product_price); ?>);
</script>

Paste this in body of individual product pages

You might also like