添加百度统计,并设置埋点

添加百度统计,并设置埋点

1、申请百度账号
2、添加站点

在这里插入图片描述

3、获取代码

在这里插入图片描述

4、index.html中添加代码
<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
    <script>
        const hostName = window.location.hostname.toLowerCase();
        // 判断正式环境才生效
        if (~hostName.indexOf('xxxxxxxxxx')){
            var _hmt = _hmt || [];
            (function() {
                var hm = document.createElement("script");
                hm.src = "https://hm.baidu.com/hm.js?xxxxxxxxxxxxxxxxxxxx";
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(hm, s);
            })();
        }
    </script>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

注意:如果代码安装正确,一般20分钟之后,可以查看网站分析数据

5、添加埋点事件
_hmt.push(['trackEvent', category, action, opt_label,opt_value]);

category: 要监控的目标的类型名称,通常是同一组目标的名字,比如”视频”、”音乐”、”软件”、”游戏”等等。该项必选。
action: 用户跟目标交互的行为,如”播放”、”暂停”、”下载”等等。该项必选。
opt_label: 事件的一些额外信息,通常可以是歌曲的名称、软件的名称、链接的名称等等。该项可选。
opt_value: 事件的一些数值信息,比如权重、时长、价格等等,在报表中可以看到其平均值等数据。该项可选。

使用:

1、行内

<a onclick="_hmt.push(['_trackEvent', 'link', 'click', 'baidutongji', 1])" href="http://www.tongji.baidu.com">百度统计</a>

2、js中(推荐自定义指令)

document.getElementById("link").addEventListener("click", function() {
  _hmt.push(['_trackEvent', 'link', 'click', 'baidutongji', 1]);
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值