-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
SessionTracker.ts
172 lines (149 loc) · 5.38 KB
/
SessionTracker.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
// the session tracker for web
import { ConsoleLogger as Logger, Hub, JS } from '@aws-amplify/core';
import { SessionTrackOpts } from '../types';
const logger = new Logger('SessionTracker');
const defaultOpts: SessionTrackOpts = {
enable: false,
provider: 'AWSPinpoint'
};
let initialEventSent = false;
export default class SessionTracker {
private _tracker;
private _hasEnabled;
private _config: SessionTrackOpts;
private _hidden;
private _visibilityChange;
constructor(tracker, opts) {
this._config = Object.assign({}, defaultOpts, opts);
this._tracker = tracker;
this._hasEnabled = false;
this._trackFunc = this._trackFunc.bind(this);
this._trackBeforeUnload = this._trackBeforeUnload.bind(this);
this.configure(this._config);
}
private _envCheck() {
if (!JS.browserOrNode().isBrowser) {
return false;
}
if (!document || !document.addEventListener) {
logger.debug('not in the supported web environment');
return false;
}
if (typeof document.hidden !== 'undefined') {
this._hidden = 'hidden';
this._visibilityChange = 'visibilitychange';
} else if (typeof document['msHidden'] !== 'undefined') {
this._hidden = 'msHidden';
this._visibilityChange = 'msvisibilitychange';
} else if (typeof document['webkitHidden'] !== 'undefined') {
this._hidden = 'webkitHidden';
this._visibilityChange = 'webkitvisibilitychange';
} else {
logger.debug('not in the supported web environment');
return false;
}
return true;
}
private async _trackFunc() {
const customAttrs = typeof this._config.attributes === 'function'?
await this._config.attributes() : this._config.attributes;
const attributes = Object.assign(
{},
customAttrs
);
if (document[this._hidden]) {
this._tracker(
{
name: '_session_stop',
attributes
},
this._config.provider
).catch(e => {
logger.debug('record session stop event failed.', e);
});
} else {
this._tracker(
{
name: '_session_start',
attributes
},
this._config.provider
).catch(e => {
logger.debug('record session start event failed.', e);
});
}
}
private async _trackBeforeUnload() {
const customAttrs = typeof this._config.attributes === 'function'?
await this._config.attributes() : this._config.attributes;
const attributes = Object.assign(
{},
customAttrs
);
this._tracker(
{
name: '_session_stop',
attributes,
immediate: true
},
this._config.provider
).catch(e => {
logger.debug('record session stop event failed.', e);
});
}
// to keep configure a synchronized function
private async _sendInitialEvent() {
if (initialEventSent) {
logger.debug('the start session has been sent when the page is loaded');
return;
} else {
initialEventSent = true;
}
const customAttrs = typeof this._config.attributes === 'function'?
await this._config.attributes() : this._config.attributes;
const attributes = Object.assign(
{},
customAttrs
);
this._tracker(
{
name: '_session_start',
attributes
},
this._config.provider
).catch(e => {
logger.debug('record session start event failed.', e);
});
}
configure(opts?: SessionTrackOpts) {
if (!this._envCheck()) {
return this._config;
}
Object.assign(this._config, opts);
if (this._config.enable && !this._hasEnabled) {
// send a start session as soon as it's enabled
this._sendInitialEvent();
// listen on events
document.addEventListener(this._visibilityChange, this._trackFunc, false);
window.addEventListener("beforeunload", this._trackBeforeUnload, false);
this._hasEnabled = true;
} else if (!this._config.enable && this._hasEnabled) {
document.removeEventListener(this._visibilityChange, this._trackFunc, false);
window.removeEventListener("beforeunload", this._trackBeforeUnload, false);
this._hasEnabled = false;
}
return this._config;
}
}