On-site SEO

Detailed explanation of Google Analytics enhanced e-commerce deployment

前段时间因为工作上的关系有幸接触到了谷歌分析(google analytics),虽然官网文档介绍的非常详细但是我在代码部署的过程中还是遇到了不少问题国内对GA代码部署的文章又非常的少所以今天把代码部署从头到尾细细道来希望网站开发人员在部署GA代码的时候能更加顺利一点

1.基础部署

first,Log inGoogle Analytics必须有一个Gmail账号所以国内的同学你懂得搞个VPN翻墙或者用google浏览器安装[谷歌访问助手]插件注册一个吧
谷歌访问助手下载http://pan.baidu.com/s/1nvNjmJJ
注册完成之后登录GA创建一个GA账户以获取一个追踪id具体操作如下

GA注册账户

GA账户注册成功之后点击管理菜单查看自己的追踪代码

GA获取追踪id

接下来直接根据提示把这些代码加入到网站页面中即可建议把追踪代码加入到公共js中在有需要用到GA的页面引入公共js
访问网页会发现报告中的网页浏览量会加1
基本语法请参考GA官方文档

2.高级功能的实现

GA的扩展性很强有很多方便我们的使用的功能。for example:自定义维度自定义指标,Target,user-id跟踪等
如果要统计该网站注册成功的用户的数量可以使用目标来实现目标在GA管理可以进行添加在目标说明可以选择目标网页或者事件这个可以根据需要进行确定

比如我在GA中设置的目标说明为事件设置事件的类型为Register_Success这就是说当网站向GA中发送这个类型的事件那么目标统计就会+1.
code:

ga(‘send’, ‘event’, ‘Register_Success’, ‘Sign Up’, RegisterName);

如果要统计用户付款时候所选择的付款方式就可以使用自定义维度来实现自定义维度也是在管理中进行添加维度的名称是GA报告中显示的名称维度的索引是网站代码中需要向GA发送数据的时候要用到

if(payMethod == 'wechat_pay'){    
ga('send', 'pageview', {
        'dimension1':  '微信支付'   //dimension1 这个1是自定义维度的索引
    });
}else{
    ga('send', 'pageview', {
        'dimension1':  '
支付宝支付'    
});
}

这时候就已经向GA中成功发送了所以为1的自定义维度了但是报告中并没有地方有展示的自定义维度的报告所以我们就可以在自定义菜单中添加一个自定义报告在自定义中找到自己的自定义维度其它配置看自己需要

接下来部署增强型电子商务

部署增强型电子商务首先需要在GA管理中开启增强电子商务功能
ps:本地环境 angular.js
添加增强电子商务插件,在公共js中加入代码: ga('require', 'ec');

1.产品列表页面所展示的产品

ga('set', '&cu', 'CNY');              // 本地货币 CNY 人民币
foods.forEach(function (data, index, array) {
    var impression = {
        'name': data.itemName,               // 产品的名称
        'id': data.itemId,                  // 产品id
        'price': data.itemPrice,            // 产品单价
        'brand': restName,                  // 品牌
        'category': categoryName,           // 分类
        'list': 'xxx Products',         // 所属的list
        'position': index               // 产品位置
    };
    ga('ec:addImpression',impression);      //注意:一个产品发送一次ec:addImpression,一个页面上有100个产品就要发送ec:addImpression 100次。
});
ga('send', 'pageview');
console.log("ga衡量商品展示");

2.衡量产品的点击

//ec:addProduct中添加被点击的商品的具体信息
 ga('ec:addProduct', {
    'id': $scope.foodData.data.itemId,
    'name':$scope.foodData.data.itemName,
    'price':$scope.foodData.data.itemPrice,
    'brand':$scope.foodData.data.branchId,
    'category': $scope.foodData.data.categoryName,
    'variant':$scope.foodData.data.desc
});
//ec:setAction click表示有产品被点击
ga('ec:setAction', 'click', {list: 'xxx Products'});
// Send click with an event, then send user to product page.
ga('send', 'event', 'UX', 'click', 'Results', {
    hitCallback: function() {
    }
});
console.log("被点击的商品"+$scope.foodData.data.itemName);

setAction操作类型

GA注册账户


3.加入购物车

ga('ec:addProduct', {
            'name': $scope.foodData.data.itemName,
            'id': $scope.foodData.data.itemId,
            'price': $scope.foodData.data.itemPrice,
            'brand': $scope.foodData.data.branchId,
            'category': $scope.foodData.data.categoryName,
            'quantity': 1
        });
        console.log("被加入购物车的商品"+$scope.foodData.data.itemName);
        ga('ec:setAction', 'add');
        ga('send', 'event', 'UX', 'click', 'add to cart');     // Send data using an event.

4.移除购物车

var products=[];
  foods.forEach(function (data,index,array) {
      var product = createProduct(data,data.itemCategory,restName);
      console.log(product);
      ga('ec:addProduct', product);
  });
  ga('ec:setAction', 'remove');
  ga('send', 'event', 'UX', 'click', 'remove from cart');     // Send data using an event.

5.点击下单按钮 开始支付流程

for(var i=0;i<$scope.cart.foods.length;i++){
       var product=  {
           'name': '',
           'id': '',
           'price': '',
           'brand': $scope.cart.restName,
           'category': 'Apparel',
           'variant': 'Black',
           'quantity': ''
       };
       product.name=$scope.cart.foods[i].itemName;
       console.log($scope.cart.foods[i].itemName);
       product.id=$scope.cart.foods[i].itemId;
       product.price=$scope.cart.foods[i].itemPrice;
       product.quantity=$scope.cart.foods[i].count;
       /*products[i] = product;*/
       ga('ec:addProduct', product);
       // products.push(product);
       console.log(product);
   }
   ga('ec:setAction','checkout', {
       'step': 1,            // A value of 1 indicates this action is first checkout step.
       'option': 'null'      // Used to specify additional info about a checkout stage, e.g. payment method.
   });
   ga('send', 'pageview');   // Pageview for payment.html

6.支付成功页面回调

//1.向GA发送已成交的的商品
 for(var i=0;i<$scope.cart.foods.length;i++){
        var product=  {
            'name': '',
            'id': '',
            'price': '',
            'brand': $scope.cart.restName,
            'category': 'Apparel',
            'variant': 'Black',
            'quantity': ''
        };
        product.name=$scope.cart.foods[i].itemName;
        console.log($scope.cart.foods[i].itemName);
        product.id=$scope.cart.foods[i].itemId;
        product.price=$scope.cart.foods[i].itemPrice;
        product.quantity=$scope.cart.foods[i].count;
        ga('ec:addProduct', product);
        console.log(product);
    }
    ga('set', '&cu', 'CNY');              // Set tracker currency to Euros
    // Transaction level information is provided via an actionFieldObject.
    ga('ec:setAction', 'purchase', {
        'id': orderNo,                         // 交易ID. Required for purchases and refunds.
        'affiliation': 'Sherpas website',
        'revenue': $scope.cart.totalPrice,                          // 收益 (incl. tax and shipping)
        'tax':'0',                                                   // 税
        'shipping': $scope.cart.deliveryFee,                     // 运送费
        'coupon': $scope.couponO
    });
    ga('send', 'pageview');     // Send transaction data with initial pageview.
    
    //2.支付流程2 支付成功
    ga('ec:setAction', 'checkout_option', {
                'step': 2,
                'option': null
            });
    ga('send', 'event', 'Checkout', 'Option', {
        hitCallback: function() {
        }
    });
    ga('send', 'pageview');     // Pageview for shipping.html

7.在GA报告电子商务管理中添加渠道

渠道1开始下单
渠道2支付成功

GA渠道配置

这样在报告–>转化–>电子商务–>购物分析–>结账行为中就可以显示出这两个渠道了
效果展示

GA渠道配置

这时候增强电子商务的大部分功能就已经实现了还有一些其它功能又上面类似就不展示了
在整个代码部署完成之后我们会发现其实这样部署代码是非常麻烦的并且需要维护起来比较困难google也是看到了这一点所以后来推出了Tag manager进行代码远程部署想要尝试用tag manager部署增强电子商务的同学可以参考这个官方已经说的很详细了

About Editorial Department

kuajinggu’s editorial staff is a team of WordPress experts,Led by Dylan,In WordPress、virtual host、e-commerce、Over 10 years of experience in SEO and marketing。kuajinggu was created in 2014 Year,Currently the largest free WordPress resource website in the industry,Often referred to as the Wikipedia of WordPress。

Leave a Reply