Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit c708542

Browse files
authored
Merge pull request #7 from chengpeiquan/develop
Develop
2 parents e2dce8f + 536a613 commit c708542

9 files changed

Lines changed: 66 additions & 13 deletions

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
vue-cnzz-analytics 使用说明
22
===
33

4-
基于Vue开发的CNZZ统计插件(友盟统计),可以在 `Vue-CLI脚手架项目` 或者 `引入了Vue相关CDN的普通页面`,以及 `VuePress`项目上使用,使用本插件的项目需要引入 `Vue Router`
4+
基于Vue开发的CNZZ统计插件(友盟统计),可以在 `Vue-CLI脚手架项目` 或者 `引入了Vue相关CDN的普通页面`,以及 `VuePress` 项目上使用,使用本插件的项目需要引入 `Vue Router`
55

66
> @v2.0版本更新:<br>最新版支持 Vue 3.x,同时兼容 Vue 2.x 使用,具体使用方法请看下方说明及demo。<br>对Vue 3.0感兴趣,但还在观望的同学,欢迎阅读我踩坑总结的:[Vue 3.0 学习教程](https://vue3.chengpeiquan.com/) (持续更新ing)
77
@@ -46,7 +46,7 @@ npm install vue-cnzz-analytics --save-dev
4646
参数|是否必填|参数类型|参数说明
4747
:-:|:-:|:-:|-
4848
router|是|object|Vue Router,本插件基于路由使用
49-
siteIdList|是|object Array|CNZZ统计的站点id列表,item为站点id<br>只有一个站点需要上报就保留一个item即可
49+
siteIdList|是|number[]|CNZZ统计的站点id列表,item为站点id<br>只有一个站点需要上报就保留一个item即可
5050
isDebug|否|boolean|是否开启debug模式,默认 `false`<br>开启后会在控制台打印上报信息,**上线前记得关闭**
5151

5252
## 使用
@@ -119,7 +119,9 @@ export default ({ Vue, router }) => {
119119
Vue.use(cnzzAnalytics, {
120120
router: router,
121121
siteIdList: [
122-
11111
122+
11111,
123+
22222,
124+
33333
123125
],
124126
isDebug: false
125127
});

dist/modules/getRouterMode.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const getRouterMode: (vueVersion: number, router: any) => string;
2+
export default getRouterMode;

dist/vue-cnzz-analytics.js

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-cnzz-analytics.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-cnzz-analytics.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-cnzz-analytics.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-cnzz-analytics",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A data collection tool that supports reporting of single-page application data built by Vue 3.0 & 2.0 & VuePress, based on cnzz statistics.",
55
"main": "dist/vue-cnzz-analytics.min.js",
66
"types": "vue-cnzz-analytics.d.ts",

src/main.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import PushCNZZ from '@m/pushCNZZ'
22
import getVueVersion from '@m/getVueVersion'
3+
import getRouterMode from '@m/getRouterMode'
34

45
/**
56
* 定义插件
@@ -50,10 +51,17 @@ export default function install (Vue: Vue, { router, siteIdList, isDebug = false
5051
* 路由切换时执行PV上报
5152
*/
5253
router.afterEach( (to: To) => {
53-
const PAGE_PATH_DIR_COUNT = window.location.pathname.split('/').length;
54-
const PAGE_PATH = window.location.pathname.split('/').slice(0, PAGE_PATH_DIR_COUNT - 1).join('/');
55-
const PAGE_URL = router.mode === 'hash' ? `${PAGE_PATH}/#${to.fullPath}` : `${PAGE_PATH}${to.fullPath}`;
54+
// 根据Vue版本获取路由模式
55+
const ROUTER_MODE: string = getRouterMode(VUE_VERSION, router);
56+
57+
// 获取页面的url信息
58+
const PAGE_PATH_DIR_COUNT: number = window.location.pathname.split('/').length;
59+
const PAGE_PATH: string = window.location.pathname.split('/').slice(0, PAGE_PATH_DIR_COUNT - 1).join('/');
60+
61+
// 根据路由模式生成要上报的链接
62+
const PAGE_URL: string = ROUTER_MODE === 'hash' ? `${PAGE_PATH}/#${to.fullPath}` : `${PAGE_PATH}${to.fullPath}`;
5663

64+
// 上报数据
5765
pushCNZZ.pv(PAGE_URL);
5866
});
5967
}

src/modules/getRouterMode.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 获取Vue的版本
3+
* @param {number} vueVersion - vue版本号,2=Vue2.x, 3=Vue3.x
4+
* @param {object} router - vue路由
5+
* @return hash=hash模式、history=history模式
6+
*/
7+
const getRouterMode = (vueVersion: number, router: any): string => {
8+
let mode: string = 'history';
9+
10+
// 2.x直接读取mode即可
11+
if ( vueVersion === 2 ) {
12+
mode = router.mode;
13+
}
14+
15+
// 3.x需要判断一下
16+
if ( vueVersion === 3 ) {
17+
const BASE: string = router.options.history.base || '';
18+
if ( BASE.includes('#') ) {
19+
mode = 'hash';
20+
}
21+
}
22+
23+
return mode;
24+
}
25+
26+
export default getRouterMode;

0 commit comments

Comments
 (0)