0

我正在尝试在商店的“谢谢”页面上设置 Google Analytics 以跟踪购买情况,但我正在努力将购买的每件商品都通过 Google Analytics 购买。

我拥有的当前代码正在获取最后的产品详细信息,但我需要所有已售商品的详细信息。

这是我正在使用的 PHP:

<? $i=1;foreach($previousorder->LineItems->OrderDetailLineItems as $order){
        $previousOrderHTML = "";
        if($i == 2){
            $i=1;
            $class = " odd";
        }else{
            $i++;
            $class = "";
        }
        if(!$order->LineDiscountAmountGross == "0"){
            $lineDiscount = sprintf('<span class="required">-%s</span>', $locale->formatCurrency($order->LineDiscountAmountGross, $nwc->getTemplateValue("lb_CurrencySymbol")));
        }else{
            $lineDiscount = "-";
        }
        $previousOrderHTML .= sprintf('<tr><td align="left" valign="top" class="extra-padding first-column details-row-one%s">%s</td><td align="left" valign="top" class="item-name extra-padding details-row-one%s">%s %s %s', $class, $order->ProductCode, $class, $order->ProductGroupName,  $order->Size,  $order->Colour);
        if(isset($order->BundleProductList->PriceListBundleProductList) && count($order->BundleProductList->PriceListBundleProductList) >= 1){
            $previousOrderHTML .= '<span class="offer-small">';
            foreach($order->BundleProductList->PriceListBundleProductList as $bundleItem){
                $previousOrderHTML .= sprintf("<br />- %s %s %s (%s) - %s %s", $bundleItem->ProductGroupName, $bundleItem->Size, $bundleItem->FabricColour, $bundleItem->ProductCode, $nwc->getValue("lb_RRP"), $locale->FormatCurrency($bundleItem->Original_GROSS_Price));
            }
            $previousOrderHTML .= '</span>';
        }
        $previousOrderHTML .= sprintf('</td><td align="left" valign="top" class="extra-padding details-row-one%s">%s</td><td align="center" valign="top" class="extra-padding details-row-one%s">%s</td><td align="right" valign="top" class="extra-padding details-row-one%s">%s</td></td><td align="right" valign="top" class="extra-padding details-row-one last-column%s">%s</td></tr>', $class, $locale->formatCurrency($order->RRP, $nwc->getTemplateValue("lb_CurrencySymbol")), $class, $order->Quantity, $class, $lineDiscount, $class, $locale->formatCurrency($order->GrossDiscountPrice, $nwc->getTemplateValue("lb_CurrencySymbol")));
        echo $previousOrderHTML;
    }
    ?>

这是将数据推送到 Google Tag Manager 和 Google Analytics 的数据层的 Javascript

<script type="text/javascript">
    $(function(){
        var cartTot = '<?=$previousorder->TotalOrderAmountExVAT;?>';
        var curr = '<?=$ShoppingCartSummary->Currency;?>';
        var ref = '<?=$previousorder->OurReferenceNumber;?>';
        var vatx = '<?=$previousorder->TotalVATAmount;?>';
        var prodid = '<?=$order->ProductCode;?>';
        var prodname = '<?=$order->ProductGroupName;?>';
        var rrp = '<?=$order->RRP;?>';
        var qnty = '<?=$order->Quantity;?>'
            dataLayer.push({
              'ecommerce': {
                'purchase': {
                  'actionField': {
                    'id': ref,                         // Transaction ID. Required for purchases and refunds.
                    'affiliation': 'Online Store',
                    'revenue': cartTot,                // Total transaction value (incl. tax and shipping)
                    'tax': vatx,
                    'shipping': '',
                  },
                  'products': [{          // List of productFieldObjects.
                    'name': prodname,     // Name or ID is required.
                    'id': prodid,
                    'price': rrp,
                    'quantity':qnty,       // Optional fields may be omitted or set to empty string.
                   }]
                }
              }
            });

请问,我该怎么做才能获得每件售出商品的产品详细信息?

非常感谢

4

1 回答 1

0

在循环中: foreach($order->BundleProductList->PriceListBundleProductList as $bundleItem){ '... } 尝试使用 PHP 在 JavaScript 变量中创建一个对象数组(productFieldObjects 列表),例如 '[{... },{...},{...}]', 例子:

[
  {'name': prodname1,
   'id': prodid1,
   'price': rrp1,
   'quantity':qnty1
   },
  {'name': prodname2,
   'id': prodid2,
   'price': rrp2,
   'quantity':qnty2
   },
  {'name': prodname3,
   'id': prodid3,
   'price': rrp3,
   'quantity':qnty3
   }
]

然后在推送中,将此值分配给“产品”键:

//...
'purchase': {
    'actionField': {
    'id': '...',
    'affiliation': '...',
    'revenue': ...,
    'tax': ...,
    'shipping': ...,
},
'products': YOUR_ARRAY_OF OBJECTS // <-- HERE
//...
于 2019-12-30T16:28:13.757 回答