John Davidson

php - Exclude value of purchase event from being sent to datalayer with GTM4WP

0 comments
Message:


I want to exclude WooCommerce revenue from the Google Analytics purchase event when the Product ID is 224112 or 159324.


I tried to find solutions to exclude sending the data to GA4 through GMT exclusion triggers or filters directly in the GA4 interface, but it seems the best way to accomplish this is to exclude the data from arriving into the datalayer in the first place.


I use the GTM4WP plugin to send the website data (WordPress, WooCommerce) to the GTM datalayer.


In the plugin, I found the part of the code that sends the purchase data to the datalayer. I'm not very familiar with this code and I dont want to mess things up on my site. Since the Google Analytics give the results only after 24 hours I want to make sure that I do the right thing right away.


This is the original plugin code:


function gtm4wp_get_purchase_datalayer( $order, $order_items ) {
global $gtm4wp_options, $gtm4wp_is_woocommerce3_7;

$dataLayer = array();

if ( $order instanceof WC_Order ) {
$woo = WC();

// variable for Google Smart Shopping campaign new customer reporting
// https://support.google.com/google-ads/answer/9917012?hl=en-AU#zippy=%2Cinstall-with-google-tag-manager
if ( $woo->customer instanceof WC_Customer ) {
// we need to use this instead of $woo->customer as this will load proper total order number and value from the database instead of the session
$woo_customer = new WC_Customer( $woo->customer->get_id() );
$dataLayer['new_customer'] = $woo_customer->get_order_count() === 1;
}

if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCEXCLUDETAX ] ) {
$order_revenue = (float)( $order->get_total() - $order->get_total_tax() );
} else {
$order_revenue = (float) $order->get_total();
}

$order_shipping_cost = (float) $order->get_shipping_total();

if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCEXCLUDESHIPPING ] ) {
$order_revenue -= $order_shipping_cost;
}

$order_currency = $order->get_currency();

if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC ] ) {
$dataLayer['event'] = 'gtm4wp.orderCompleted';
$dataLayer['transactionId'] = $order->get_order_number();
$dataLayer['transactionAffiliation'] = '';
$dataLayer['transactionTotal'] = $order_revenue;
$dataLayer['transactionShipping'] = $order_shipping_cost;
$dataLayer['transactionTax'] = (float) $order->get_total_tax();
$dataLayer['transactionCurrency'] = $order_currency;
}

if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) {
$dataLayer['event'] = 'gtm4wp.orderCompletedEEC';
$dataLayer['ecommerce'] = array(
'currencyCode' => $order_currency,
'purchase' => array(
'actionField' => array(
'id' => $order->get_order_number(),
'affiliation' => '',
'revenue' => $order_revenue,
'tax' => (float) $order->get_total_tax(),
'shipping' => (float)( $order->get_shipping_total() ),
'coupon' => implode( ', ', ( $gtm4wp_is_woocommerce3_7 ? $order->get_coupon_codes() : $order->get_used_coupons() ) ),
)
)
);
}

if ( isset( $order_items ) ) {
$_order_items = $order_items;
} else {
$_order_items = gtm4wp_process_order_items( $order );
}

if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC ] ) {
$dataLayer['transactionProducts'] = $_order_items['products'];
}

if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) {
$dataLayer['ecommerce']['purchase']['products'] = $_order_items['products'];
}

if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) {
$dataLayer['ecomm_prodid'] = $_order_items['product_ids'];
$dataLayer['ecomm_pagetype'] = 'purchase';
$dataLayer['ecomm_totalvalue'] = (float) $_order_items['sumprice'];
}
}

return $dataLayer;
}

I think I could add an if statement just before the excludeTAX option. It would be something like this:


If ($productID = array (224112, 159324)) {
$order_revenue = 0;
} else {
$order_revenue = (float) $order->get_total();
}

I don't know what would be the correct variable for $productID and I am not sure that this would work.


Any help is appreciated.



My Answer:
You are on the right track with your approach. You can add an if statement to check if the Product ID is 224112 or 159324 and set the order revenue to 0 in that case. Here is how you can modify the code:

php
if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCEXCLUDETAX ] ) {
$product_ids_to_exclude = array(224112, 159324);

$exclude_revenue = false;
foreach ( $order_items as $item ) {
if ( in_array( $item['product_id'], $product_ids_to_exclude ) ) {
$exclude_revenue = true;
break;
}
}

if ( $exclude_revenue ) {
$order_revenue = 0;
} else {
$order_revenue = (float) $order->get_total();
}
} else {
$order_revenue = (float) $order->get_total();
}


In this code snippet, we first define an array `$product_ids_to_exclude` containing the Product IDs that we want to exclude from the revenue calculation. Then, we loop through the order items and check if any of the product IDs match the ones in the exclusion array. If a match is found, we set `$exclude_revenue` to true and set the order revenue to 0. Otherwise, we calculate the order revenue as usual.

Make sure to test this code thoroughly on a staging site before implementing it on your live site to ensure that it works as expected.

Rate this post

3 of 5 based on 9924 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us