Skip to content

Commit d485018

Browse files
committed
chore(vue): adjust directory structure
1 parent 5142203 commit d485018

3 files changed

Lines changed: 84 additions & 83 deletions

File tree

packages/vue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web-analytics/vue",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Website pageview analytics tool for Vue.js and multi-analytics-platform support.",
55
"author": "chengpeiquan <chengpeiquan@chengpeiquan.com>",
66
"license": "MIT",

packages/vue/src/index.ts

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1 @@
1-
import { isBrowser } from '@bassist/utils'
2-
import { VueAnalytics } from './analytics'
3-
import { getMajorVersion, getGlobalProperty } from './utils'
4-
import type { Platform } from '@web-analytics/core'
5-
import type {
6-
CreateVueAnalyticsInstanceOptions,
7-
InstallerProviderOptions,
8-
VueInstance,
9-
} from './types'
10-
11-
function installerProvider<P extends Platform>({
12-
analytics,
13-
platform,
14-
}: InstallerProviderOptions<P>) {
15-
return {
16-
install: (
17-
app: VueInstance,
18-
{ router, ...others }: CreateVueAnalyticsInstanceOptions
19-
) => {
20-
if (!isBrowser) return
21-
22-
// Synchronize the real properties with the prototype to the exported instance
23-
const analyticsInstance = new VueAnalytics<P>({ platform, ...others })
24-
for (const key in analyticsInstance) {
25-
if (Object.prototype.hasOwnProperty.call(analyticsInstance, key)) {
26-
// @ts-ignore
27-
analytics[key] = analyticsInstance[key]
28-
}
29-
}
30-
const prototype = Object.getPrototypeOf(analyticsInstance)
31-
Object.setPrototypeOf(analytics, prototype)
32-
33-
// Add a global property to Vue instance
34-
const property = getGlobalProperty(platform)
35-
const version = getMajorVersion(app)
36-
switch (version) {
37-
case 2: {
38-
app.prototype[property] = analytics
39-
break
40-
}
41-
case 3: {
42-
app.config.globalProperties[property] = analytics
43-
break
44-
}
45-
}
46-
47-
// Automatic tracking of pageview
48-
if (!router) return
49-
router.afterEach(() => {
50-
analytics.trackPageview({
51-
pageUrl: window.location.href,
52-
})
53-
})
54-
},
55-
}
56-
}
57-
58-
export function createVueBaiduAnalytics() {
59-
// @ts-ignore
60-
const baiduAnalytics: VueAnalytics<'baidu'> = {}
61-
62-
return {
63-
baiduAnalytics,
64-
installVueBaiduAnalytics: installerProvider({
65-
analytics: baiduAnalytics,
66-
platform: 'baidu',
67-
}),
68-
}
69-
}
70-
71-
export function createVueCnzzAnalytics() {
72-
// @ts-ignore
73-
const cnzzAnalytics: VueAnalytics<'cnzz'> = {}
74-
75-
return {
76-
cnzzAnalytics,
77-
installVueCnzzAnalytics: installerProvider({
78-
analytics: cnzzAnalytics,
79-
platform: 'cnzz',
80-
}),
81-
}
82-
}
1+
export * from './variants/vue'

packages/vue/src/variants/vue.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { isBrowser } from '@bassist/utils'
2+
import { VueAnalytics } from '../analytics'
3+
import { getMajorVersion, getGlobalProperty } from '../utils'
4+
import type { Platform } from '@web-analytics/core'
5+
import type {
6+
CreateVueAnalyticsInstanceOptions,
7+
InstallerProviderOptions,
8+
VueInstance,
9+
} from '../types'
10+
11+
function installerProvider<P extends Platform>({
12+
analytics,
13+
platform,
14+
}: InstallerProviderOptions<P>) {
15+
return {
16+
install: (
17+
app: VueInstance,
18+
{ router, ...others }: CreateVueAnalyticsInstanceOptions
19+
) => {
20+
if (!isBrowser) return
21+
22+
// Synchronize the real properties with the prototype to the exported instance
23+
const analyticsInstance = new VueAnalytics<P>({ platform, ...others })
24+
for (const key in analyticsInstance) {
25+
if (Object.prototype.hasOwnProperty.call(analyticsInstance, key)) {
26+
// @ts-ignore
27+
analytics[key] = analyticsInstance[key]
28+
}
29+
}
30+
const prototype = Object.getPrototypeOf(analyticsInstance)
31+
Object.setPrototypeOf(analytics, prototype)
32+
33+
// Add a global property to Vue instance
34+
const property = getGlobalProperty(platform)
35+
const version = getMajorVersion(app)
36+
switch (version) {
37+
case 2: {
38+
app.prototype[property] = analytics
39+
break
40+
}
41+
case 3: {
42+
app.config.globalProperties[property] = analytics
43+
break
44+
}
45+
}
46+
47+
// Automatic tracking of pageview
48+
if (!router) return
49+
router.afterEach(() => {
50+
analytics.trackPageview({
51+
pageUrl: window.location.href,
52+
})
53+
})
54+
},
55+
}
56+
}
57+
58+
export function createVueBaiduAnalytics() {
59+
// @ts-ignore
60+
const baiduAnalytics: VueAnalytics<'baidu'> = {}
61+
62+
return {
63+
baiduAnalytics,
64+
installVueBaiduAnalytics: installerProvider({
65+
analytics: baiduAnalytics,
66+
platform: 'baidu',
67+
}),
68+
}
69+
}
70+
71+
export function createVueCnzzAnalytics() {
72+
// @ts-ignore
73+
const cnzzAnalytics: VueAnalytics<'cnzz'> = {}
74+
75+
return {
76+
cnzzAnalytics,
77+
installVueCnzzAnalytics: installerProvider({
78+
analytics: cnzzAnalytics,
79+
platform: 'cnzz',
80+
}),
81+
}
82+
}

0 commit comments

Comments
 (0)