我创建了用于测试电子商务跟踪的新页面。它显示页面视图,但不显示任何交易和添加到仪表板上的项目。

这是我的脚本,该脚本添加在页面的<body>标记中。

属性以异步加载脚本。 ->



<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'trackingcode', 'auto');
ga('require', 'ecommerce');

ga('ecommerce:addTransaction', {
  'id': '1234',
  'affiliation': 'Acme Clothing',
  'revenue': '11.99',
  'shipping': '5',
  'tax': '1.29',
});
ga('ecommerce:send');
</script>

<script>
ga('require', 'ecommerce')
ga('ecommerce:addItem', {
    'id': '1234',
    'name': 'Fluffy Pink Bunnies',
    'sku': 'DD23444',
    'category': 'Party Toys',
    'price': '11.99',
    'quantity': '1',
});
ga('ecommerce:send');
ga('send', 'pageview');
</script>


<head>标记中,我添加了以下代码:

<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'UA-116940006-1');
</script>

最佳答案

您必须在标题中添加带有@yield名称的google-ecommerce

@yield('google-ecommerce')


然后在您的Thank you page(交易后的最后一页)中添加以下代码:

@extends('front.layouts.master')


@section('google-ecommerce')
    <script>
        gtag('event', 'purchase', {
            "affiliation": "Google online store",
            "value": 23.07,
            "currency": "USD",
            "tax": 1.24,
            "transaction_id": '{{ $pay->invoice_num }}',
            "shipping"      : '{{ $pay->price }}',
            "items"         : [
                {
                    "id"      : '{{ $pay->id }}',
                    "name"    : '{{ $pay->title }}',
                    "category": '{{ $pay->category }}',
                    "quantity": 1,
                    "price"   : '{{ $pay->price }}'
                }
            ]
        });
    </script>
@endsection



@section('content')
    //-------
    //-------
@endsection


看到这个链接:

Enhanced ecommerce with gtag.js

10-07 22:07