• Hi,

    I configured a custom metric (product scoped) in my GA account that should hold the purchase price of any product.

    add_filter(‘gtm4wp_eec_product_array’, ‘inbound_gtm_product’);
    function inbound_gtm_product($array)
    {
    $product = get_product($array[‘id’]);
    // cost of goods
    $array[‘metric1’] = get_post_meta($array[‘id’], ‘yith_cog_cost’, true);
    return $array;
    }

    I managed to push this to GA with the code above but the issue I’m having is that it’s also pushing when the product is viewed/added to cart. So it get’s included in my profit calculation although a purchase didn’t happen.

    Is there a way to only set this custom metric on the purchase event?

    Kind regards

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    This hook as a second parameter that you can use to filter only the purchase data.
    Just add a second parameter which will be a string and hold the value “purchase” in your case. Anything else in this parameter can be ommited.

    Thread Starter inboundbe

    (@inboundbe)

    I’m not entirely sure if I get it correctly but would something like this work?

    add_filter('gtm4wp_eec_product_array', 'inbound_gtm_product', 10, 2);
    function inbound_gtm_product($array, $event)
    {
        $product = get_product($array['id']);
    
        // cost of goods
        if ($event === 'purchase') {
            $array['metric1'] = get_post_meta($array['id'], 'yith_cog_cost', true);
        }
        return $array;
    }
    

    Greetings

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Yes, that should work.

    But I would move the get_product() call into the if() statement to save some resources in case the user is not on the purchase page.

    Thread Starter inboundbe

    (@inboundbe)

    Yes, I’m aware of that. But I probably will be adding some other values that should be pushed always.

    by the way, I’m actually still noticing some weird behavior after applying above behavior. I still see the cost of goods getting applied multiple times.
    I have the impression this metric also gets pushed when someone in the backend processes/finishes the order.

    The metric actually should only get pushed when a customer completes an order in the frontend :/

    In Analytics I’m doing profit/margin calculators based on this “costs of good” value but right now I’m experiencing large negative values for this which is ofc impossible ?? So for some reason the value get pushed multiple times.

    Thread Starter inboundbe

    (@inboundbe)

    I’ll take some of that back ?? It doesn’t seem to happen when an order gets processed/finished in the backend.

    I changed my approach and created a new custom metric to start with a clean slate.

    1. I did a data import in GA consisting of 2 columns: ga:productSku and ga:metric2 (which holds the individual cost of goods of each product)
    2. As GA now knows the “costs of good” of each product I don’t supply it to the datalayer myself anymore but instead I set the value of metric2 to 0 when we’re not on a purchase page.

    add_filter('gtm4wp_eec_product_array', 'inbound_gtm_product', 10, 2);
    function inbound_gtm_product($array, $event)
    {
        if ($event !== 'purchase') {
            $array['metric3'] = 0;
        }
        return $array;
    }

    If I debug in GTM I see that this value is properly added to the datalayer.
    But for some reason GTM doesn’t respect that 0 value, could it be?

    3. Now after a couple of hours I see again lots of products with a “costs of goods” that is higher than the product revenue …
    F.e. a product with an individual “costs of good” of 10.70 has been sold 3x but GA gives a value of 74.90 instead of 32.10.

    Any idea what I could be doing wrong?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Push custom metric only on purchase event’ is closed to new replies.