|
| 1 | +# @web-analytics/vue |
| 2 | + |
| 3 | +Website pageview analytics tool for [Vue.js](https://vuejs.org) and multi-analytics-platform support. |
| 4 | + |
| 5 | +With [Vue Router](https://router.vuejs.org) , It helps you quickly collect pageviews on your website, including single-page web applications. |
| 6 | + |
| 7 | +:::tip |
| 8 | +This is a Vue.js-specific package that can only be used in Vue.js-based web projects (supports Vue 2 and Vue 3). |
| 9 | +::: |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Installed into `dependencies` of package.json in the project. |
| 14 | + |
| 15 | +:::code-group |
| 16 | + |
| 17 | +```bash [npm] |
| 18 | +npm install @web-analytics/vue |
| 19 | +``` |
| 20 | + |
| 21 | +```bash [yarn] |
| 22 | +yarn add @web-analytics/vue |
| 23 | +``` |
| 24 | + |
| 25 | +```bash [pnpm] |
| 26 | +pnpm add @web-analytics/vue |
| 27 | +``` |
| 28 | + |
| 29 | +::: |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +It is recommended to initialize in public tool files such as utils and export the initialized instance. |
| 34 | + |
| 35 | +:::code-group |
| 36 | + |
| 37 | +```ts [Baidu Analytics] |
| 38 | +// @/libs/analytics.ts |
| 39 | +import { createVueBaiduAnalytics } from '@web-analytics/vue' |
| 40 | + |
| 41 | +// The Create function returns an instance variable, |
| 42 | +// And a method of installation for plugin |
| 43 | +const { |
| 44 | + // The instance of baidu analytics platform, |
| 45 | + // At this time it does not work properly, |
| 46 | + // You need to register through `installVueBaiduAnalytics` |
| 47 | + // Make it do the initialization |
| 48 | + baiduAnalytics, |
| 49 | + |
| 50 | + // Used in the entry file (e.g. `main.ts` ), |
| 51 | + // Will be registered as a Vue plugin and enabled |
| 52 | + installVueBaiduAnalytics, |
| 53 | +} = createVueBaiduAnalytics() |
| 54 | + |
| 55 | +// Export them both |
| 56 | +export { baiduAnalytics, installVueBaiduAnalytics } |
| 57 | +``` |
| 58 | + |
| 59 | +```ts [CNZZ Analytics] |
| 60 | +// @/libs/analytics.ts |
| 61 | +import { createVueCnzzAnalytics } from '@web-analytics/vue' |
| 62 | + |
| 63 | +// The Create function returns an instance variable, |
| 64 | +// And a method of installation for plugin |
| 65 | +const { |
| 66 | + // The instance of cnzz analytics platform, |
| 67 | + // At this time it does not work properly, |
| 68 | + // You need to register through `installVueCnzzAnalytics` |
| 69 | + // Make it do the initialization |
| 70 | + cnzzAnalytics, |
| 71 | + |
| 72 | + // Used in the entry file (e.g. `main.ts` ), |
| 73 | + // Will be registered as a Vue plugin and enabled |
| 74 | + installVueCnzzAnalytics, |
| 75 | +} = createVueCnzzAnalytics() |
| 76 | + |
| 77 | +// Export them both |
| 78 | +export { cnzzAnalytics, installVueCnzzAnalytics } |
| 79 | +``` |
| 80 | + |
| 81 | +::: |
| 82 | + |
| 83 | +Import the installation method in your project's entry file (e.g. `main.ts` ) and use it. |
| 84 | + |
| 85 | +:::code-group |
| 86 | + |
| 87 | +```ts [Baidu Analytics] |
| 88 | +// @/main.ts |
| 89 | +import { createApp } from 'vue' |
| 90 | +import App from '@/App.vue' |
| 91 | +import router from '@/router' |
| 92 | +import { installVueBaiduAnalytics } from '@/libs/analytics' |
| 93 | + |
| 94 | +// Create a Vue instance like any other Vue project |
| 95 | +const app = createApp(App) |
| 96 | + |
| 97 | +app |
| 98 | + // Enable Vue routing |
| 99 | + .use(router) |
| 100 | + // Enable baidu analytics |
| 101 | + .use(installVueBaiduAnalytics, { |
| 102 | + // If you provide a router instance, |
| 103 | + // You can get the ability to automatically track pageviews |
| 104 | + router, |
| 105 | + // Some options... |
| 106 | + }) |
| 107 | + .mount('#app') |
| 108 | +``` |
| 109 | + |
| 110 | +```ts [CNZZ Analytics] |
| 111 | +// @/main.ts |
| 112 | +import { createApp } from 'vue' |
| 113 | +import App from '@/App.vue' |
| 114 | +import router from '@/router' |
| 115 | +import { installVueCnzzAnalytics } from '@/libs/analytics' |
| 116 | + |
| 117 | +// Create a Vue instance like any other Vue project |
| 118 | +const app = createApp(App) |
| 119 | + |
| 120 | +app |
| 121 | + // Enable Vue routing |
| 122 | + .use(router) |
| 123 | + // Enable cnzz analytics |
| 124 | + .use(installVueCnzzAnalytics, { |
| 125 | + // If you provide a router instance, |
| 126 | + // You can get the ability to automatically track pageviews |
| 127 | + router, |
| 128 | + // Some options... |
| 129 | + }) |
| 130 | + .mount('#app') |
| 131 | +``` |
| 132 | + |
| 133 | +::: |
| 134 | + |
| 135 | +For more plugin options of `installVueBaiduAnalytics` , please refer to [the initialization section](#initialization) . |
| 136 | + |
| 137 | +If you provide a router instance, You can get the ability to automatically track pageviews. So in most cases you don't need to actively call its method. |
| 138 | + |
| 139 | +If it is really necessary, it can be called like this. |
| 140 | + |
| 141 | +In other files, import the instance and call the method on the instance. |
| 142 | + |
| 143 | +:::code-group |
| 144 | + |
| 145 | +```ts [Baidu Analytics] |
| 146 | +// @/foo.ts |
| 147 | +import { baiduAnalytics } from '@/libs/analytics' |
| 148 | + |
| 149 | +// For more methods, please see the documentation below |
| 150 | +baiduAnalytics.trackPageview({ |
| 151 | + // Some options... |
| 152 | +}) |
| 153 | +``` |
| 154 | + |
| 155 | +```ts [CNZZ Analytics] |
| 156 | +// @/foo.ts |
| 157 | +import { cnzzAnalytics } from '@/libs/analytics' |
| 158 | + |
| 159 | +// For more methods, please see the documentation below |
| 160 | +cnzzAnalytics.trackPageview({ |
| 161 | + // Some options... |
| 162 | +}) |
| 163 | +``` |
| 164 | + |
| 165 | +::: |
| 166 | + |
| 167 | +For more detailed instructions, continue reading the documentation. |
| 168 | + |
| 169 | +## Platforms |
| 170 | + |
| 171 | +The following analytics platforms are currently supported. |
| 172 | + |
| 173 | +- Type Declarations: |
| 174 | + |
| 175 | +```ts |
| 176 | +type Platform = |
| 177 | + /** |
| 178 | + * Baidu analysis platform |
| 179 | + * @website https://tongji.baidu.com |
| 180 | + * @docs https://tongji.baidu.com/open/api |
| 181 | + */ |
| 182 | + | 'baidu' |
| 183 | + |
| 184 | + /** |
| 185 | + * U-Web(CNZZ) analysis platform |
| 186 | + * @website https://www.umeng.com/web |
| 187 | + * @docs https://developer.umeng.com/docs/67963/detail/74517 |
| 188 | + */ |
| 189 | + | 'cnzz' |
| 190 | +``` |
| 191 | + |
| 192 | +:::tip |
| 193 | +Because the developer of the plugin lives in mainland China, so the current priority is to support the two major analytics platforms in China. |
| 194 | +::: |
| 195 | + |
| 196 | +## Initialization |
| 197 | + |
| 198 | +The plugin provides a method for creating an instance for each platform, and only needs to pass in the necessary options to complete the initialization work. |
| 199 | + |
| 200 | +- Type Declaration of methods: |
| 201 | + |
| 202 | +:::code-group |
| 203 | + |
| 204 | +```ts [Baidu Analytics] |
| 205 | +export declare function createVueBaiduAnalytics(): { |
| 206 | + /** |
| 207 | + * The instance of cnzz analytics platform, |
| 208 | + * It will be initialized when `installVueBaiduAnalytics` starts working |
| 209 | + */ |
| 210 | + baiduAnalytics: VueAnalytics<'baidu'> |
| 211 | + |
| 212 | + /** |
| 213 | + * It Will be registered as a Vue plugin, |
| 214 | + * And initialize the `baiduAnalytics` instance, |
| 215 | + * You must used in the entry file (e.g. `main.ts` ) |
| 216 | + */ |
| 217 | + installVueBaiduAnalytics: { |
| 218 | + install: ( |
| 219 | + app: VueInstance, |
| 220 | + { router, ...others }: CreateVueAnalyticsInstanceOptions |
| 221 | + ) => void |
| 222 | + } |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +```ts [CNZZ Analytics] |
| 227 | +export declare function createVueCnzzAnalytics(): { |
| 228 | + /** |
| 229 | + * The instance of cnzz analytics platform, |
| 230 | + * It will be initialized when `installVueCnzzAnalytics` starts working |
| 231 | + */ |
| 232 | + cnzzAnalytics: VueAnalytics<'cnzz'> |
| 233 | + |
| 234 | + /** |
| 235 | + * It Will be registered as a Vue plugin, |
| 236 | + * And initialize the `cnzzAnalytics` instance, |
| 237 | + * You must used in the entry file (e.g. `main.ts` ) |
| 238 | + */ |
| 239 | + installVueCnzzAnalytics: { |
| 240 | + install: ( |
| 241 | + app: VueInstance, |
| 242 | + { router, ...others }: CreateVueAnalyticsInstanceOptions |
| 243 | + ) => void |
| 244 | + } |
| 245 | +} |
| 246 | +``` |
| 247 | + |
| 248 | +::: |
| 249 | + |
| 250 | +- Type Declaration of options: |
| 251 | + |
| 252 | +```ts |
| 253 | +export interface CreateVueAnalyticsInstanceOptions { |
| 254 | + /** |
| 255 | + * Instance of Vue Router, |
| 256 | + * after passing this option, when the route changes, |
| 257 | + * the plugin will automatically track the pageview |
| 258 | + */ |
| 259 | + router?: VueRouter |
| 260 | + |
| 261 | + /** |
| 262 | + * This is an array that supports multiple analytics platform IDs |
| 263 | + */ |
| 264 | + websiteIds: WebsiteId[] |
| 265 | + |
| 266 | + /** |
| 267 | + * Whether to enable debug mode |
| 268 | + */ |
| 269 | + debug?: boolean |
| 270 | +} |
| 271 | +``` |
| 272 | + |
| 273 | +The JS-SDK file of the analytics platform will be loaded during initialization. |
| 274 | + |
| 275 | +## Methods |
| 276 | + |
| 277 | +On the initialized instance, some APIs are provided to call. |
| 278 | + |
| 279 | +### trackPageview |
| 280 | + |
| 281 | +The Vue package maintains the same API as the Core package. |
| 282 | + |
| 283 | +Please refer to [the trackPageview document of the core package](../core/index.md#trackpageview) for details. |
| 284 | + |
| 285 | +### trackEvent |
| 286 | + |
| 287 | +The Vue package maintains the same API as the Core package. |
| 288 | + |
| 289 | +Please refer to [the trackEvent document of the core package](../core/index.md#trackevent) for details. |
0 commit comments