如何在 laravel 项目中启用和实施 google 电子商务跟踪

How to Enable and implement google ecommerce tracking in laravel project

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

这是我添加到页面 <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>

您必须在 header:

中添加 @yieldgoogle-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

看到这个 link :

Enhanced ecommerce with gtag.js