小程序谷歌统计 Google Analytics

npm install @ouduidui/ga-tracker

  1. 在跟踪器上设置自定义跟踪服务器 tracker.setTrackerServer("https://ga-proxy.example.com")
  2. 修改域名example.com的DNS记录,将 ga-proxy.example.com 指向你自己的服务器IP
  3. 自己服务器上的nginx做如下配置
  4. upstream real_ga_servers {
            server www.google-analytics.com:443 weight=5 max_fails=0;
            keepalive 64;
    }
    
    server {    
       listen 443 ssl;
       server_name ga-proxy.example.com;
    
       ssl on;
       # 设置ssl证书(略)
    
        location / {
    
            # 告诉ga用户真实ip
            rewrite ^(.*)$   $1?uip=$remote_addr    break;
    
            proxy_set_header   Host       www.google-analytics.com;
    
            # Proxy to google-analytics.com
             proxy_buffering off;
    
            # 使用keepalive
             proxy_http_version 1.1; # require  nginx > 1.1.4
             proxy_set_header Connection ""; # for keepalive upstream
    
             proxy_pass https://real_ga_servers;
             proxy_redirect off;
    
        }
    }

    main.js引入

import {GoogleAnalytics} from "@ouduidui/ga-tracker";

const gaTracker = {
    instance: null,
    getInstance() {
        if (!this.instance) {
            // 初始化GoogleAnalytics Tracker
            this.instance = GoogleAnalytics.getInstance() 
                .setAppName('UniApp')   // 设置名称
                .setAppVersion('1.0.0')   // 设置版本号
                .newTracker('UA-XXXXXX-X');  // 设置跟踪ID

            // #ifdef MP-WEIXIN
            // 使用自己的合法域名做跟踪数据转发
            this.instance.setTrackerServer("https://ga-proxy.example.com");
            // #endif
        }
        return this.instance;
    },
}

Vue.prototype.$gaTracker = gaTracker;

 使用方法

<template>
    <view class="content">
        <image class="logo" src="/static/logo.png"></image>
        <view>
            <text class="title">{{title}}</text>
        </view>
    </view>
</template>

<script>
    import {HitBuilders}  from "@ouduidui/ga-tracker";

    export default {
        data() {
            return {
                title: 'Hello'
            }
        },
        () {
            // 设置屏幕名称
        const t = this.$gaTracker.getInstance()
            t.setScreenName('这是我的首屏页');
            // 或者对应相应字段
            t.set('xx',val)
            // 上报
            t.send(new HitBuilders.ScreenViewBuilder().build());
        }
    }
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值