0
votes

I want to push the products profit of a transaction to Google Analytics. With this i want to generate a report where i can see the total Product revenue and it's profit for each product that has been ordered.

i'm using this api to push custom metrics to analytics (Measurement protocol api)

I have made several custom metrics in analytics, for example:

  • Productsprofit (Scope hit, decimal)
  • Productsprofit2 (Scope Product, decimal)

I've googled several times and i think i need to use the Scope Hit for this type of tracking, am i right?

I've used the api to push the metric with the right indexes to Analytics

$analytics->setTransactionId('test'.$orderId)
        ->setAffiliation('Testshop')
        ->setRevenue(number_format($orderTotalPrice,2,'.',''))
        ->setTax(number_format($taxTotal,2,'.',''))
        ->setShipping(number_format($shippingTotal,2,'.',''))
        ->setCouponCode('');

    foreach($orderInfo['orderDetails'] as $orderDetail) {

        // Include a product, only required fields are SKU and Name
        $productData1 = [
            'sku' => $orderDetail['model'],
            'name' => $orderDetail['name'],
            'brand' => '',
            'category' => '',
            'variant' => '',
            'price' => number_format($orderDetail['price'], 2, '.', ''),
            'quantity' => $orderDetail['aantal'],
            'coupon_code' => '',
            'position' => 0,
            'custom_metric_3'=>50.45, //<-- test data (Hit)
            'custom_metric_4'=>15.50 //<-- test data (Product)
        ];

        $analytics->addProduct($productData1);
    }
// Don't forget to set the product action, in this case to PURCHASE
    $analytics->setProductActionToPurchase();

// Finally, you must send a hit, in this case we send an Event
    $analytics->setEventCategory('Checkout')
        ->setEventAction('Purchase')
        ->sendEvent();

I've created a custom report and added these metric to my report. But both metrics stay empty in my report.

Does anyone know what i'm doing wrong ? or what might solve my problem?

Thanks in advance!

1
If you want to send Custom Metrics through the measurment protocoll it should look like this cm1=. How do you build the call? developers.google.com/analytics/devguides/collection/protocol/…DanielS
i'm using this api github.com/theiconic/php-ga-measurement-protocol . I don't think you need to add cm1= for this in this api.Marco

1 Answers

0
votes

In the PHP library that you are using, custom metrics and dimensions are set in a different manner depending on the scope, for Product scope you use the product array like you did, for Hit scope you use the setCustomMetric method. In your code this would look like:

$analytics->setTransactionId('test'.$orderId)
    ->setAffiliation('Testshop')
    ->setRevenue(number_format($orderTotalPrice,2,'.',''))
    ->setTax(number_format($taxTotal,2,'.',''))
    ->setShipping(number_format($shippingTotal,2,'.',''));

foreach($orderInfo['orderDetails'] as $orderDetail) {

    $productData1 = [
        'sku' => $orderDetail['model'],
        'name' => $orderDetail['name'],
        'brand' => 'the brand',
        'category' => 'test category',
        'variant' => 'variant x',
        'price' => number_format($orderDetail['price'], 2, '.', ''),
        'quantity' => $orderDetail['qty'],
        'coupon_code' => 'coupon used',
        'custom_metric_4' => 15.50,
    ];

    $analytics->addProduct($productData1);
}

$analytics->setProductActionToPurchase();

$analytics->setCustomMetric(50.45, 3);

$analytics->setEventCategory('Checkout')
    ->setEventAction('Purchase')
    ->sendEvent();

Notice the call to $analytics->setCustomMetric(50.45, 3); before sending the hit. This should send the right data, the rest is creating or looking at the right report in Google Analytics.