Google Analytics自定义指标未显示

时间:2017-05-26 10:05:04

标签: google-analytics analytics metric

我想将交易的产品利润推向Google Analytics。 有了这个,我想生成一个报告,我可以看到已经订购的每个产品的总产品收入和利润。

我正在使用此API将自定义指标推送到分析(Measurement protocol api

我在分析中制作了多个自定义指标,例如:

  • 产品利润(范围命中,小数)
  • Productsprofit2(范围产品,小数)

我已经多次使用Google搜索,我认为我需要使用Scope Hit进行此类跟踪,我是对的吗?

我已经使用api将带有正确索引的指标推送到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();

我已创建自定义报告,并将这些指标添加到我的报告中。但在我的报告中,这两个指标都是空的。

有谁知道我做错了什么?或者什么可以解决我的问题?

提前致谢!

1 个答案:

答案 0 :(得分:0)

在您使用的PHP库中,自定义指标和维度根据范围以不同方式设置,对于您使用产品数组的产品范围,就像您使用setCustomMetric方法。在您的代码中,这将是:

$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();

在发送匹配之前,请注意对$analytics->setCustomMetric(50.45, 3);的呼叫。这应该发送正确的数据,其余的是在Google Analytics中创建或查看正确的报告。