@github/hydro-analytics-client 2.3.3 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analytics-client.d.ts +14 -0
- package/dist/analytics-client.js +75 -0
- package/package.json +1 -1
|
@@ -3,14 +3,28 @@ export interface AnalyticsClientOptions {
|
|
|
3
3
|
collectorUrl: string;
|
|
4
4
|
clientId?: string;
|
|
5
5
|
baseContext?: Context;
|
|
6
|
+
maxBatchSize?: number;
|
|
7
|
+
idleTimeout?: number;
|
|
6
8
|
}
|
|
7
9
|
export declare class AnalyticsClient {
|
|
8
10
|
readonly options: AnalyticsClientOptions;
|
|
11
|
+
private eventBatch;
|
|
12
|
+
private idleCallbackId;
|
|
13
|
+
private fallbackTimerId;
|
|
9
14
|
constructor(options: AnalyticsClientOptions);
|
|
10
15
|
get collectorUrl(): string;
|
|
11
16
|
get clientId(): string;
|
|
17
|
+
private get maxBatchSize();
|
|
18
|
+
private get idleTimeout();
|
|
12
19
|
private createEvent;
|
|
13
20
|
sendPageView(context?: Context): void;
|
|
14
21
|
sendEvent(type: string, context: Context): void;
|
|
22
|
+
sendBatchedEvent(type: string, context: Context): void;
|
|
23
|
+
flushBatch(): void;
|
|
24
|
+
destroy(): void;
|
|
25
|
+
private scheduleFlush;
|
|
26
|
+
private cancelScheduledFlush;
|
|
27
|
+
private onVisibilityChange;
|
|
28
|
+
private boundFlush;
|
|
15
29
|
private send;
|
|
16
30
|
}
|
package/dist/analytics-client.js
CHANGED
|
@@ -4,6 +4,21 @@ import { getOrCreateClientId } from './client-id.js';
|
|
|
4
4
|
export class AnalyticsClient {
|
|
5
5
|
constructor(options) {
|
|
6
6
|
this.options = options;
|
|
7
|
+
this.eventBatch = [];
|
|
8
|
+
this.idleCallbackId = null;
|
|
9
|
+
this.fallbackTimerId = null;
|
|
10
|
+
this.onVisibilityChange = () => {
|
|
11
|
+
if (document.visibilityState === 'hidden') {
|
|
12
|
+
this.flushBatch();
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
this.boundFlush = () => this.flushBatch();
|
|
16
|
+
if (typeof document !== 'undefined') {
|
|
17
|
+
document.addEventListener('visibilitychange', this.onVisibilityChange);
|
|
18
|
+
}
|
|
19
|
+
if (typeof window !== 'undefined') {
|
|
20
|
+
window.addEventListener('pagehide', this.boundFlush);
|
|
21
|
+
}
|
|
7
22
|
}
|
|
8
23
|
get collectorUrl() {
|
|
9
24
|
return this.options.collectorUrl;
|
|
@@ -14,6 +29,12 @@ export class AnalyticsClient {
|
|
|
14
29
|
}
|
|
15
30
|
return getOrCreateClientId();
|
|
16
31
|
}
|
|
32
|
+
get maxBatchSize() {
|
|
33
|
+
return this.options.maxBatchSize ?? 10;
|
|
34
|
+
}
|
|
35
|
+
get idleTimeout() {
|
|
36
|
+
return this.options.idleTimeout ?? 1000;
|
|
37
|
+
}
|
|
17
38
|
createEvent(context) {
|
|
18
39
|
return {
|
|
19
40
|
page: location.href,
|
|
@@ -36,6 +57,60 @@ export class AnalyticsClient {
|
|
|
36
57
|
};
|
|
37
58
|
this.send({ events: [event] });
|
|
38
59
|
}
|
|
60
|
+
sendBatchedEvent(type, context) {
|
|
61
|
+
const event = {
|
|
62
|
+
...this.createEvent(context),
|
|
63
|
+
type
|
|
64
|
+
};
|
|
65
|
+
this.eventBatch.push(event);
|
|
66
|
+
if (this.eventBatch.length >= this.maxBatchSize) {
|
|
67
|
+
this.flushBatch();
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
this.scheduleFlush();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
flushBatch() {
|
|
74
|
+
if (this.eventBatch.length === 0)
|
|
75
|
+
return;
|
|
76
|
+
this.cancelScheduledFlush();
|
|
77
|
+
const events = this.eventBatch;
|
|
78
|
+
this.eventBatch = [];
|
|
79
|
+
this.send({ events });
|
|
80
|
+
}
|
|
81
|
+
destroy() {
|
|
82
|
+
this.flushBatch();
|
|
83
|
+
if (typeof document !== 'undefined') {
|
|
84
|
+
document.removeEventListener('visibilitychange', this.onVisibilityChange);
|
|
85
|
+
}
|
|
86
|
+
if (typeof window !== 'undefined') {
|
|
87
|
+
window.removeEventListener('pagehide', this.boundFlush);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
scheduleFlush() {
|
|
91
|
+
if (this.idleCallbackId !== null || this.fallbackTimerId !== null)
|
|
92
|
+
return;
|
|
93
|
+
if (typeof requestIdleCallback === 'function') {
|
|
94
|
+
this.idleCallbackId = requestIdleCallback(this.boundFlush, {
|
|
95
|
+
timeout: this.idleTimeout
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.fallbackTimerId = setTimeout(this.boundFlush, this.idleTimeout);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
cancelScheduledFlush() {
|
|
103
|
+
if (this.idleCallbackId !== null) {
|
|
104
|
+
if (typeof cancelIdleCallback === 'function') {
|
|
105
|
+
cancelIdleCallback(this.idleCallbackId);
|
|
106
|
+
}
|
|
107
|
+
this.idleCallbackId = null;
|
|
108
|
+
}
|
|
109
|
+
if (this.fallbackTimerId !== null) {
|
|
110
|
+
clearTimeout(this.fallbackTimerId);
|
|
111
|
+
this.fallbackTimerId = null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
39
114
|
send({ page_views, events }) {
|
|
40
115
|
const payload = {
|
|
41
116
|
client_id: this.clientId,
|