123456789101112131415161718192021222324252627282930313233343536373839 |
- export default function (c, i) {
- const { app, $auth, store } = c;
- window._hmt = window._hmt || [];
- window._hmt.push(['_setAutoPageview', false]);
- window._hmt.push(['_setCustomVar', 1, 'MODE', process.env.MODE, 1]);
- app.router.afterEach((to, from) => {
- // 上报PV
- window._hmt.push(['_trackPageview', c.base + to.path.slice(1)]);
- });
- app.head.script = app.head.script || [];
- app.head.script.push({
- src: `https://hm.baidu.com/hm.js?${process.env.BAIDU_TONGJI_KEY}`,
- id: 'baidu-tongji',
- });
- // $auth.
- const setUserId = () => {
- window._hmt.push(['_setCustomVar', 2, 'userId', $auth.user?.id ?? 0, 2]);
- };
- store.watch(
- (state, getter) => {
- return $auth.user;
- },
- (user) => setUserId,
- {},
- );
- setUserId();
- i('tongji', {
- // 上报pv
- trackPageview: (path) => window._hmt.push(['_trackPageview', path]),
- // 上报事件
- trackEvent: (category, action, optLabel, optValue) =>
- window._hmt.push(['_trackEvent', category, action, optLabel, optValue]),
- });
- }
|