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

Commit caa1d42

Browse files
authored
Merge pull request #5 from chengpeiquan/develop
Develop
2 parents 7b7e48d + 25193f9 commit caa1d42

22 files changed

+624
-292
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ vue-cnzz-analytics 使用说明
33

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

6+
> @v2.0版本更新:<br>最新版支持 Vue 3.x,同时兼容 Vue 2.x 使用,具体使用方法请看下方说明及demo。<br>对Vue 3.0感兴趣,但还在观望的同学,欢迎阅读我踩坑总结的:[Vue 3.0 学习教程](https://vue3.chengpeiquan.com/) (持续更新ing)
7+
68
## 功能
79

810
* 异步载入CNZZ统计脚本,不再因等待加载统计代码而影响页面渲染速度
@@ -15,34 +17,38 @@ vue-cnzz-analytics 使用说明
1517

1618
* 支持手动提交事件分析上报
1719

20+
* 自动识别Vue版本,自动适配Vue 2.0/3.0使用(插件2.0版本新增)
21+
1822
## 预览
1923

2024
demo已开启debug模式,可开启控制台查看上报情况。
2125

22-
点击预览:[vue-cnzz-analytics demo](https://chengpeiquan.github.io/vue-cnzz-analytics/demo/ "vue-cnzz-analytics demo")
23-
24-
## 参数
26+
Vue 2.0 版本:[vue-cnzz-analytics demo for Vue 2.x](https://chengpeiquan.github.io/vue-cnzz-analytics/demo/vue2.html "vue-cnzz-analytics demo for Vue 2.x")
2527

26-
参数|是否必填|参数类型|参数说明
27-
:-:|:-:|:-:|-
28-
router|是|object|Vue Router,本插件基于路由使用
29-
siteIdList|是|object Array|CNZZ统计的站点id列表,item为站点id<br>只有一个站点需要上报就保留一个item即可
30-
isDebug|否|boolean|是否开启debug模式,默认 `false`<br>开启后会在控制台打印上报信息,**上线前记得关闭**
28+
Vue 3.0 版本:[vue-cnzz-analytics demo for Vue 3.x](https://chengpeiquan.github.io/vue-cnzz-analytics/demo/vue3.html "vue-cnzz-analytics demo for Vue 3.x")
3129

3230
## 安装
3331

34-
### 通过npm安装
32+
方式一:通过npm安装
3533

3634
```
3735
npm install vue-cnzz-analytics --save-dev
3836
```
3937

40-
### 通过cdn安装
38+
方式二:通过cdn安装
4139

4240
```html
4341
<script src="https://cdn.jsdelivr.net/npm/vue-cnzz-analytics/dist/vue-cnzz-analytics.min.js"></script>
4442
```
4543

44+
## 参数
45+
46+
参数|是否必填|参数类型|参数说明
47+
:-:|:-:|:-:|-
48+
router|是|object|Vue Router,本插件基于路由使用
49+
siteIdList|是|object Array|CNZZ统计的站点id列表,item为站点id<br>只有一个站点需要上报就保留一个item即可
50+
isDebug|否|boolean|是否开启debug模式,默认 `false`<br>开启后会在控制台打印上报信息,**上线前记得关闭**
51+
4652
## 使用
4753

4854
通过npm安装的项目,需要先在 `main.js` 里引入插件(通过cdn则无需该步骤)。
@@ -51,10 +57,14 @@ npm install vue-cnzz-analytics --save-dev
5157
import cnzzAnalytics from 'vue-cnzz-analytics'
5258
```
5359

54-
安装插件后,在 `main.js` 引入以下代码,即可开启自动上报功能,首次访问页面会部署统计代码并提交第一次访问数据上报。
60+
安装插件后,在 `main.js` 引入以下代码(注意区分Vue2.0和Vue3.0的用法区别),即可开启自动上报功能,首次访问页面会部署统计代码并提交第一次访问数据上报。
5561

5662
后续在路由切换过程中,也会根据路由的切换提交相应的url信息到友盟统计。
5763

64+
### 在 Vue 2.0 里使用
65+
66+
可参考demo:[main.js - Vue 2.0 demo](https://chengpeiquan.github.io/vue-cnzz-analytics/demo/js/main-for-vue2.js)
67+
5868
```js
5969
Vue.use(cnzzAnalytics, {
6070
router: router,
@@ -67,6 +77,33 @@ Vue.use(cnzzAnalytics, {
6777
});
6878
```
6979

80+
### 在 Vue 3.0 里使用
81+
82+
可参考demo:[main.js - Vue 3.0 demo](https://chengpeiquan.github.io/vue-cnzz-analytics/demo/js/main-for-vue3.js)
83+
84+
```js
85+
/**
86+
* 初始化Vue
87+
*/
88+
createApp(app)
89+
// 启动路由
90+
.use(router)
91+
92+
// 启动插件
93+
.use(cnzzAnalytics, {
94+
router: router,
95+
siteIdList: [
96+
11111,
97+
22222,
98+
33333
99+
],
100+
isDebug: true
101+
})
102+
103+
// 挂载到节点上
104+
.mount('#app');
105+
```
106+
70107
可在开发环境打开debug模式了解相关的上报情况(上线前记得关闭debug)。
71108

72109
## 方法
@@ -90,17 +127,26 @@ fromUrl|否|String|来路页面的url,必须是以 `http` 或 `https` 开头
90127

91128
**使用示范**
92129

93-
在template里使用(第二个参数不填表示直接输入地址访问)
130+
在 Vue 2.0 里使用
94131

95-
```html
96-
<button @click="$pushCNZZ.pv('/test')">手动上报PV</button>
132+
```js
133+
this.$pushCNZZ.pv(
134+
this.pageUrl,
135+
this.fromUrl
136+
);
97137
```
98138

99-
在method里使用(第二个参数填写,则表示是从该页面跳转过来的)
139+
在 Vue 3.0 里使用
140+
141+
(使用3.0的生命周期,需要遵循Vue3的规范,引入一个Vue自带的代理组件,并写在 `setup` 里执行)
100142

101143
```js
102-
// this是Vue实例
103-
this.$pushCNZZ.pv('/home', 'https://github.com/chengpeiquan/vue-cnzz-analytics');
144+
const { proxy } = getCurrentInstance();
145+
146+
proxy.$pushCNZZ.pv(
147+
pageUrl.value,
148+
fromUrl.value
149+
);
104150
```
105151

106152
### 手动上报事件分析
@@ -121,15 +167,30 @@ nodeId|否|string|产生该事件的元素id,默认为空
121167

122168
**使用示范**
123169

124-
在template里使用(比如:点击了一个id为123的首页banner)
170+
在 Vue 2.0 里使用
125171

126-
```html
127-
<button @click="$pushCNZZ.event('首页banner', '点击', 'bannerId_123')">手动上报点击事件</button>
172+
```js
173+
this.$pushCNZZ.event(
174+
this.category,
175+
this.action,
176+
this.label,
177+
this.value,
178+
this.nodeId
179+
);
128180
```
129181

130-
在method里使用(比如:点击了一个id为123的首页banner,并设置该事件的价值为1)
182+
在 Vue 3.0 里使用
183+
184+
(使用3.0的生命周期,需要遵循Vue3的规范,引入一个Vue自带的代理组件,并写在 `setup` 里执行)
131185

132186
```js
133-
// this是Vue实例
134-
this.$pushCNZZ.event('首页banner', '点击', 'bannerId_123', 1);
187+
const { proxy } = getCurrentInstance();
188+
189+
proxy.$pushCNZZ.event(
190+
category.value,
191+
action.value,
192+
label.value,
193+
value.value,
194+
nodeId.value
195+
);
135196
```

demo/css/style.css

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#app,
2+
.section {
3+
display: flex;
4+
flex-direction: column;
5+
justify-content: flex-start;
6+
align-items: center;
7+
}
8+
.section {
9+
margin-bottom: 40px;
10+
}
11+
.title {
12+
font-size: 40px;
13+
color: #000;
14+
}
15+
.link {
16+
color: #666;
17+
text-decoration: none;
18+
}
19+
.link:hover {
20+
color: #000;
21+
}
22+
.nav {
23+
margin-bottom: 20px;
24+
}
25+
.nav .item {
26+
color: #666;
27+
margin: 0 10px 20px;
28+
}
29+
.nav .cur {
30+
color: #000;
31+
font-weight: bold;
32+
}
33+
.label {
34+
display: flex;
35+
justify-content: center;
36+
align-items: center;
37+
margin-bottom: 20px;
38+
}
39+
.text {
40+
display: flex;
41+
justify-content: flex-end;
42+
align-items: center;
43+
width: 60px;
44+
font-size: 14px;
45+
color: #333;
46+
margin-right: 20px;
47+
}
48+
.input {
49+
display: flex;
50+
align-items: center;
51+
width: 240px;
52+
height: 40px;
53+
font-size: 14px;
54+
box-sizing: border-box;
55+
padding: 0 10px;
56+
}
57+
.button {
58+
padding: 5px 20px;
59+
cursor: pointer;
60+
}

demo/js/main-for-vue2.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* 初始化路由
3+
* routes是来自 js/routes.js 里面的配置
4+
*/
5+
const router = new VueRouter({
6+
routes,
7+
linkActiveClass: 'cur',
8+
linkExactActiveClass: 'cur'
9+
});
10+
11+
12+
/**
13+
* 引入统计插件
14+
*/
15+
Vue.use(cnzzAnalytics, {
16+
router: router,
17+
siteIdList: [
18+
11111,
19+
22222,
20+
33333
21+
],
22+
isDebug: true
23+
});
24+
25+
26+
/**
27+
* 初始化Vue
28+
*/
29+
const app = new Vue({
30+
el: '#app',
31+
router,
32+
data () {
33+
return {
34+
pageUrl: '',
35+
fromUrl: '',
36+
category: '',
37+
action: '',
38+
label: '',
39+
value: '',
40+
nodeId: ''
41+
}
42+
},
43+
mounted () {
44+
},
45+
methods: {
46+
pv () {
47+
this.$pushCNZZ.pv(
48+
this.pageUrl,
49+
this.fromUrl
50+
);
51+
},
52+
event () {
53+
this.$pushCNZZ.event(
54+
this.category,
55+
this.action,
56+
this.label,
57+
this.value,
58+
this.nodeId
59+
);
60+
}
61+
}
62+
});

0 commit comments

Comments
 (0)