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

Commit d7bfd85

Browse files
authored
Merge pull request #9 from analyticsjs/develop
Release v2.1.0
2 parents 047a6f1 + c4351c2 commit d7bfd85

25 files changed

Lines changed: 3040 additions & 2332 deletions

.eslintrc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
browser: true
5+
},
6+
parser: '@typescript-eslint/parser',
7+
parserOptions: {
8+
ecmaVersion: 2020
9+
},
10+
plugins: [
11+
'@typescript-eslint'
12+
],
13+
rules: {}
14+
};

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ community_bridge: # Replace with a single Community Bridge project-name e.g., cl
99
liberapay: # Replace with a single Liberapay username
1010
issuehunt: # Replace with a single IssueHunt username
1111
otechie: # Replace with a single Otechie username
12-
custom: https://git.io/Sponsor
12+
custom: [chengpeiquan](https://github.com/chengpeiquan/sponsor)

README.md

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

4-
基于Vue开发的百度统计插件,可以在 `Vue-CLI脚手架项目` 或者 `引入了Vue相关CDN的普通页面`,以及 `VuePress` 项目上使用,使用本插件的项目需要引入 `Vue Router`
4+
基于 Vue 开发的百度统计插件,可以在 `Vue-CLI 脚手架项目` 或者 `引入了 Vue 相关 CDN 的普通页面`,以及 `VuePress` 项目上使用,使用本插件的项目需要引入 `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)
6+
> v2.0 版本更新:<br>最新版支持 Vue 3.x,同时兼容 Vue 2.x 使用,具体使用方法请看下方说明及demo。<br>对 Vue 3.0 感兴趣,但还在观望的同学,欢迎阅读我踩坑总结的:[Vue 3.0 学习教程](https://vue3.chengpeiquan.com/) (持续更新ing)
77
88
## 功能
99

@@ -29,13 +29,13 @@ Vue 3.0 版本:[vue-baidu-analytics demo for Vue 3.x](https://chengpeiquan.git
2929

3030
## 安装
3131

32-
方式一:通过npm安装
32+
方式一:通过 NPM 安装
3333

3434
```
3535
npm install vue-baidu-analytics --save-dev
3636
```
3737

38-
方式二:通过cdn安装
38+
方式二:通过 CDN 安装
3939

4040
```html
4141
<script src="https://cdn.jsdelivr.net/npm/vue-baidu-analytics/dist/vue-baidu-analytics.min.js"></script>
@@ -46,20 +46,20 @@ npm install vue-baidu-analytics --save-dev
4646
参数|是否必填|参数类型|参数说明
4747
:-:|:-:|:-:|-
4848
router|是|object|Vue Router,本插件基于路由使用
49-
siteIdList|是|string[]|百度统计的站点id列表,item为站点id<br>只有一个站点需要上报就保留一个item即可
50-
isDebug|否|boolean|是否开启debug模式,默认 `false`<br>开启后会在控制台打印上报信息,**上线前记得关闭**
49+
siteIdList|是|string[]|百度统计的站点 id 列表,item 为站点 id <br>只有一个站点需要上报就保留一个 item 即可
50+
isDebug|否|boolean|是否开启 debug 模式,默认 `false`<br>开启后会在控制台打印上报信息,**上线前记得关闭**
5151

5252
## 使用
5353

54-
通过npm安装的项目,需要先在 `main.js` 里引入插件(通过cdn则无需该步骤)。
54+
通过 NPM 安装的项目,需要先在 `main.js` 里引入插件(通过 CDN 则无需该步骤)。
5555

5656
```js
5757
import baiduAnalytics from 'vue-baidu-analytics'
5858
```
5959

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

62-
后续在路由切换过程中,也会根据路由的切换提交相应的url信息到友盟统计
62+
后续在路由切换过程中,也会根据路由的切换提交相应的url信息到百度统计
6363

6464
### 在 Vue 2.0 里使用
6565

@@ -128,21 +128,21 @@ export default ({ Vue, router }) => {
128128
};
129129
```
130130

131-
可在开发环境打开debug模式了解相关的上报情况(上线前记得关闭debug)。
131+
可在开发环境打开 debug 模式了解相关的上报情况(上线前记得关闭 debug )。
132132

133133
## 方法
134134

135-
插件目前封装了两个常用的api,统一挂载到Vue实例上的 `$pushBAIDU` 去调用。
135+
插件目前封装了两个常用的 API ,统一挂载到 Vue 实例上的 `$pushBAIDU` 去调用。
136136

137-
注:如果配置了多个站点id,会同时上报给所有站点。
137+
注:如果配置了多个站点 id ,会同时上报给所有站点。
138138

139-
### 手动上报页面PV
139+
### 手动上报页面 PV
140140

141-
api名称|功能说明
141+
API 名称|功能说明
142142
:-:|-
143143
pv|手动执行PV数据上报
144144

145-
**api参数**
145+
**API 参数**
146146

147147
参数|是否必填|参数类型|参数说明
148148
:-:|:-:|:-:|-
@@ -158,7 +158,7 @@ this.$pushBAIDU.pv(this.pageUrl);
158158

159159
在 Vue 3.0 里使用
160160

161-
使用3.0的生命周期,需要遵循Vue3的规范,引入一个Vue自带的代理组件,并写在 `setup` 里执行)
161+
使用 3.0 的生命周期,需要遵循 Vue3 的规范,引入一个 Vue 自带的代理组件,并写在 `setup` 里执行)
162162

163163
```js
164164
const { proxy } = getCurrentInstance();
@@ -168,18 +168,18 @@ proxy.$pushBAIDU.pv(pageUrl.value);
168168

169169
### 手动上报事件分析
170170

171-
api名称|功能说明
171+
API 名称|功能说明
172172
:-:|-
173173
event|手动执行事件分析数据上报
174174

175-
**api参数**
175+
**API 参数**
176176

177177
参数|是否必填|参数类型|参数说明
178178
:-:|:-:|:-:|-
179179
category|是|string|产生该事件的位置名称,比如 `首页banner`
180180
action|是|string|产生该事件的行为描述,比如 `点击`
181-
label|否|string|产生该事件的标签名称,可以用来记录事件子id,比如 `bannerId_123`,默认为空
182-
value|否|number|该事件的分值,默认0
181+
label|否|string|产生该事件的标签名称,可以用来记录事件子 id,比如 `bannerId_123`,默认为空
182+
value|否|number|该事件的分值,默认 0
183183

184184
**使用示范**
185185

@@ -196,7 +196,7 @@ this.$pushBAIDU.event(
196196

197197
在 Vue 3.0 里使用
198198

199-
使用3.0的生命周期,需要遵循Vue3的规范,引入一个Vue自带的代理组件,并写在 `setup` 里执行)
199+
使用 3.0 的生命周期,需要遵循 Vue3 的规范,引入一个 Vue 自带的代理组件,并写在 `setup` 里执行)
200200

201201
```js
202202
const { proxy } = getCurrentInstance();
@@ -207,4 +207,4 @@ proxy.$pushBAIDU.event(
207207
label.value,
208208
value.value
209209
);
210-
```
210+
```

demo/js/main-for-vue2.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* 初始化路由
33
* routes是来自 js/routes.js 里面的配置
44
*/
@@ -9,21 +9,21 @@ const router = new VueRouter({
99
});
1010

1111

12-
/**
12+
/**
1313
* 引入统计插件
14+
* @description 自 v2.1.0 版本开始,需要使用 .default 去激活插件
1415
*/
15-
Vue.use(baiduAnalytics, {
16+
Vue.use(baiduAnalytics.default, {
1617
router: router,
1718
siteIdList: [
1819
'aaaaaaaaaaaaaaaaaaa',
19-
'bbbbbbbbbbbbbbbbbbb',
20-
'ccccccccccccccccccc'
20+
'bbbbbbbbbbbbbbbbbbb'
2121
],
2222
isDebug: true
2323
});
2424

2525

26-
/**
26+
/**
2727
* 初始化Vue
2828
*/
2929
const app = new Vue({
@@ -35,22 +35,48 @@ const app = new Vue({
3535
category: '',
3636
action: '',
3737
label: '',
38-
value: ''
38+
value: '',
39+
40+
// 也可以绑定一个钩子变量去使用
41+
baidu: baiduAnalytics.usePush()
3942
}
4043
},
4144
mounted () {
45+
this.baidu.pv('/use-push-api/?from=mounted');
4246
},
4347
methods: {
48+
/**
49+
* 提交 pv
50+
* @description 支持两种推送方式
51+
*/
4452
pv () {
53+
// 使用默认全局 API
4554
this.$pushBAIDU.pv(this.pageUrl);
55+
56+
// 使用钩子 API
57+
// this.baidu.pv(this.pageUrl);
4658
},
59+
60+
/**
61+
* 提交事件
62+
* @description 支持两种推送方式
63+
*/
4764
event () {
48-
this.$pushBAIDU.event(
65+
// 使用默认全局 API
66+
// this.$pushBAIDU.event(
67+
// this.category,
68+
// this.action,
69+
// this.label,
70+
// this.value
71+
// );
72+
73+
// 使用钩子 API
74+
this.baidu.event(
4975
this.category,
5076
this.action,
5177
this.label,
5278
this.value
5379
);
5480
}
5581
}
56-
});
82+
});

demo/js/main-for-vue3.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/**
1+
/**
22
* 导入需要用到的组件
33
*/
44
const { createRouter, createWebHashHistory } = VueRouter;
55
const { createApp, getCurrentInstance, ref } = Vue;
6+
const { usePush } = baiduAnalytics;
67

78

8-
/**
9+
/**
910
* 初始化路由
1011
* routes是来自 js/routes.js 里面的配置
1112
*/
@@ -17,13 +18,22 @@ const router = createRouter({
1718
});
1819

1920

20-
/**
21+
/**
2122
* 创建实例
2223
*/
2324
const app = {
2425
setup () {
25-
// 插件要用到的一个代理组件
26-
const { proxy } = getCurrentInstance();
26+
/**
27+
* 新的推荐方式
28+
* @description 创建一个钩子变量去使用,更适合于 TypeScript 项目
29+
*/
30+
const baidu = usePush();
31+
32+
/**
33+
* 原来的方式
34+
* @description 用代理组件去操作,对 TS 项目不够友好
35+
*/
36+
// const { proxy } = getCurrentInstance();
2737

2838
// 初始化要用到的数据
2939
const pageUrl = ref('');
@@ -32,19 +42,38 @@ const app = {
3242
const label = ref('');
3343
const value = ref('');
3444

35-
// 提交pv的操作
45+
/**
46+
* 提交 pv
47+
* @description 支持两种推送方式
48+
*/
3649
const pv = () => {
37-
proxy.$pushBAIDU.pv(pageUrl.value);
50+
// 通过钩子去操作
51+
baidu.pv(pageUrl.value);
52+
53+
// 通过代理组件去操作
54+
// proxy.$pushBAIDU.pv(pageUrl.value);
3855
}
3956

40-
// 提交事件的操作
57+
/**
58+
* 提交事件
59+
* @description 支持两种推送方式
60+
*/
4161
const event = () => {
42-
proxy.$pushBAIDU.event(
62+
// 通过钩子去操作
63+
baidu.event(
4364
category.value,
4465
action.value,
4566
label.value,
4667
value.value
4768
);
69+
70+
// 通过代理组件去操作
71+
// proxy.$pushBAIDU.event(
72+
// category.value,
73+
// action.value,
74+
// label.value,
75+
// value.value
76+
// );
4877
}
4978

5079
// Vue 3.0 需要把模板要用到的东西 return 出去
@@ -64,23 +93,22 @@ const app = {
6493
};
6594

6695

67-
/**
96+
/**
6897
* 初始化Vue
6998
*/
7099
createApp(app)
71100
// 启动路由
72101
.use(router)
73102

74103
// 启动插件
75-
.use(baiduAnalytics, {
104+
.use(baiduAnalytics.default, {
76105
router: router,
77106
siteIdList: [
78107
'aaaaaaaaaaaaaaaaaaa',
79-
'bbbbbbbbbbbbbbbbbbb',
80-
'ccccccccccccccccccc'
108+
'bbbbbbbbbbbbbbbbbbb'
81109
],
82110
isDebug: true
83111
})
84-
112+
85113
// 挂载到节点上
86-
.mount('#app');
114+
.mount('#app');

demo/vue2.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>[Vue2] vue baidu analytics demo</title>
7-
<script src="https://unpkg.com/vue@2"></script>
8-
<script src="https://unpkg.com/vue-router@3"></script>
9-
<link rel="stylesheet" href="css/style.css">
7+
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
8+
<script src="https://cdn.jsdelivr.net/npm/vue-router@3"></script>
9+
<link rel="stylesheet" href="./css/style.css">
1010
</head>
1111
<body>
1212

@@ -59,8 +59,8 @@ <h2>提交event测试</h2>
5959
</div>
6060

6161
<script src="../dist/vue-baidu-analytics.js"></script>
62-
<script src="js/routes.js"></script>
63-
<script src="js/main-for-vue2.js"></script>
64-
62+
<script src="./js/routes.js"></script>
63+
<script src="./js/main-for-vue2.js"></script>
64+
6565
</body>
66-
</html>
66+
</html>

0 commit comments

Comments
 (0)