-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.page.ts
62 lines (59 loc) · 1.44 KB
/
plugin.page.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* The plugin to capture the events when the user first visits the page and when the user leaves the page.
* Scope:
* - First time page load
* - When page unload
* - Browser history change
* @extends PluginBuilder
*/
import { BaseOptions, BindResult } from '../../types'
import { PluginBuilder } from '../core'
export default class PageViewPlugin extends PluginBuilder {
override key: string = 'page'
override bind(_: BaseOptions): BindResult[] {
return [
// Detect when the user first visits the page on first load
{
name: 'page',
target: window,
event: 'load',
callback: () => {
return this.captureEvent('page-load')
},
options: {
once: true,
capture: true
}
},
// Detect when the user leaves the page
{
name: 'page',
target: window,
event: 'beforeunload',
callback: () => {
return this.captureEvent('page-leave')
}
},
// Detect when the history state changes
{
name: 'page',
target: window,
event: 'popstate',
callback: () => {
return this.captureEvent('page-change')
},
options: {
capture: true
}
}
]
}
/**
* A function to capture the page view events on your site.
*/
private captureEvent(state: string): Record<string, any> {
return {
type: state
}
}
}