如何在拉拉维尔项目中启用和实施谷歌电子商务跟踪



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

这是我的脚本,它被添加到页面<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>

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

@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

请参阅此链接 :

使用 gtag 增强电子商务.js

最新更新