@amplitude/analytics-core 2.61.0-video-analytics-6.0 → 2.62.0-codexsemantic-review-integration.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/lib/cjs/config.d.ts +0 -1
- package/lib/cjs/config.d.ts.map +1 -1
- package/lib/cjs/config.js +0 -1
- package/lib/cjs/config.js.map +1 -1
- package/lib/cjs/heartbeat.d.ts +3 -7
- package/lib/cjs/heartbeat.d.ts.map +1 -1
- package/lib/cjs/heartbeat.js +8 -81
- package/lib/cjs/heartbeat.js.map +1 -1
- package/lib/cjs/index.d.ts +3 -1
- package/lib/cjs/index.d.ts.map +1 -1
- package/lib/cjs/index.js +12 -3
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/observers/video.d.ts +3 -6
- package/lib/cjs/observers/video.d.ts.map +1 -1
- package/lib/cjs/observers/video.js +8 -41
- package/lib/cjs/observers/video.js.map +1 -1
- package/lib/cjs/page-view-id.d.ts +17 -0
- package/lib/cjs/page-view-id.d.ts.map +1 -0
- package/lib/cjs/page-view-id.js +131 -0
- package/lib/cjs/page-view-id.js.map +1 -0
- package/lib/cjs/plugins/destination.d.ts +1 -21
- package/lib/cjs/plugins/destination.d.ts.map +1 -1
- package/lib/cjs/plugins/destination.js +25 -176
- package/lib/cjs/plugins/destination.js.map +1 -1
- package/lib/cjs/sdk-initialization-offset.d.ts +10 -0
- package/lib/cjs/sdk-initialization-offset.d.ts.map +1 -0
- package/lib/cjs/sdk-initialization-offset.js +55 -0
- package/lib/cjs/sdk-initialization-offset.js.map +1 -0
- package/lib/cjs/types/config/core-config.d.ts +0 -5
- package/lib/cjs/types/config/core-config.d.ts.map +1 -1
- package/lib/cjs/types/config/core-config.js.map +1 -1
- package/lib/cjs/types/config/react-native-config.d.ts.map +1 -1
- package/lib/cjs/types/config/react-native-config.js.map +1 -1
- package/lib/cjs/types/event/base-event.d.ts +0 -1
- package/lib/cjs/types/event/base-event.d.ts.map +1 -1
- package/lib/cjs/types/event/base-event.js.map +1 -1
- package/lib/cjs/types/event/event.d.ts +1 -4
- package/lib/cjs/types/event/event.d.ts.map +1 -1
- package/lib/cjs/types/event/event.js.map +1 -1
- package/lib/cjs/types/payload.d.ts +0 -5
- package/lib/cjs/types/payload.d.ts.map +1 -1
- package/lib/cjs/types/payload.js.map +1 -1
- package/lib/cjs/video-analytics/track-video.d.ts +1 -1
- package/lib/cjs/video-analytics/track-video.d.ts.map +1 -1
- package/lib/cjs/video-analytics/track-video.js +19 -107
- package/lib/cjs/video-analytics/track-video.js.map +1 -1
- package/lib/cjs/video-analytics/types.d.ts +5 -6
- package/lib/cjs/video-analytics/types.d.ts.map +1 -1
- package/lib/cjs/video-analytics/types.js.map +1 -1
- package/lib/esm/config.d.ts +0 -1
- package/lib/esm/config.d.ts.map +1 -1
- package/lib/esm/config.js +0 -1
- package/lib/esm/config.js.map +1 -1
- package/lib/esm/heartbeat.d.ts +3 -7
- package/lib/esm/heartbeat.d.ts.map +1 -1
- package/lib/esm/heartbeat.js +8 -81
- package/lib/esm/heartbeat.js.map +1 -1
- package/lib/esm/index.d.ts +3 -1
- package/lib/esm/index.d.ts.map +1 -1
- package/lib/esm/index.js +2 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/observers/video.d.ts +3 -6
- package/lib/esm/observers/video.d.ts.map +1 -1
- package/lib/esm/observers/video.js +8 -41
- package/lib/esm/observers/video.js.map +1 -1
- package/lib/esm/page-view-id.d.ts +17 -0
- package/lib/esm/page-view-id.d.ts.map +1 -0
- package/lib/esm/page-view-id.js +125 -0
- package/lib/esm/page-view-id.js.map +1 -0
- package/lib/esm/plugins/destination.d.ts +1 -21
- package/lib/esm/plugins/destination.d.ts.map +1 -1
- package/lib/esm/plugins/destination.js +26 -177
- package/lib/esm/plugins/destination.js.map +1 -1
- package/lib/esm/sdk-initialization-offset.d.ts +10 -0
- package/lib/esm/sdk-initialization-offset.d.ts.map +1 -0
- package/lib/esm/sdk-initialization-offset.js +50 -0
- package/lib/esm/sdk-initialization-offset.js.map +1 -0
- package/lib/esm/types/config/core-config.d.ts +0 -5
- package/lib/esm/types/config/core-config.d.ts.map +1 -1
- package/lib/esm/types/config/core-config.js.map +1 -1
- package/lib/esm/types/config/react-native-config.d.ts.map +1 -1
- package/lib/esm/types/config/react-native-config.js.map +1 -1
- package/lib/esm/types/event/base-event.d.ts +0 -1
- package/lib/esm/types/event/base-event.d.ts.map +1 -1
- package/lib/esm/types/event/base-event.js.map +1 -1
- package/lib/esm/types/event/event.d.ts +1 -4
- package/lib/esm/types/event/event.d.ts.map +1 -1
- package/lib/esm/types/event/event.js.map +1 -1
- package/lib/esm/types/payload.d.ts +0 -5
- package/lib/esm/types/payload.d.ts.map +1 -1
- package/lib/esm/types/payload.js.map +1 -1
- package/lib/esm/video-analytics/track-video.d.ts +1 -1
- package/lib/esm/video-analytics/track-video.d.ts.map +1 -1
- package/lib/esm/video-analytics/track-video.js +19 -107
- package/lib/esm/video-analytics/track-video.js.map +1 -1
- package/lib/esm/video-analytics/types.d.ts +5 -6
- package/lib/esm/video-analytics/types.d.ts.map +1 -1
- package/lib/esm/video-analytics/types.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video.js","sourceRoot":"","sources":["../../../src/observers/video.ts"],"names":[],"mappings":";;;;AAAA,8DAAoF;
|
|
1
|
+
{"version":3,"file":"video.js","sourceRoot":"","sources":["../../../src/observers/video.ts"],"names":[],"mappings":";;;;AAAA,8DAAoF;AA8BpF;IAwCE,uBAAY,EAAmE;YAAjE,OAAO,aAAA,EAAE,aAAa,mBAAA,EAAE,MAAM,YAAA,EAAE,UAAU,gBAAA;QAAxD,iBAOC;QA9CO,UAAK,GAAU;YACrB,aAAa,EAAE,QAAQ;SACxB,CAAC;QAIM,YAAO,GAAiB;YAC9B,MAAM,EAAE,UAAC,GAAe;gBACtB,KAAI,CAAC,mBAAmB,CAAC,SAAS,EAAE,GAAG,CAAC,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mCAAI,CAAC,CAAC,GAAG,SAAS,GACnD,CAAC;QACF,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IAC9B,CAAC;IAEO,mCAAW,GAAnB,UAAoB,SAAgB;QAClC,IAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC;QACjC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC;QACvB,IAAI,CAAC,kBAAkB,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IACpD,CAAC;IAED,+BAAO,GAAP;QACE,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;IACH,oBAAC;AAAD,CAAC,AA9GD,IA8GC;AA9GY,sCAAa","sourcesContent":["import { trackHtmlVideo, trackEmbeddedVideo } from '../video-analytics/track-video';\nimport type {\n VideoHandler,\n VideoEvent,\n EmbeddedVideoPlayer,\n MuxElement,\n Vendor,\n TimeUpdateEvent,\n} from '../video-analytics/types';\n\nexport type { Vendor };\n\ntype PlaybackState = 'playing' | 'paused' | 'ended' | 'error' | 'seeking';\n\nexport type State = {\n playbackState: PlaybackState;\n errorMessage?: string;\n lastEvent?: VideoEvent;\n watchTime?: number;\n position?: number | null;\n isSeeking?: boolean;\n};\n\nexport type VideoObserverParams = {\n videoEl: HTMLVideoElement | EmbeddedVideoPlayer | MuxElement;\n onStateChange: (previousState: State, nextState: State) => void;\n vendor?: Vendor;\n isEmbedded?: boolean;\n};\n\nexport class VideoObserver {\n private state: State = {\n playbackState: 'paused',\n };\n\n private untrack: () => void;\n private onStateChange: (previousState: State, nextState: State) => void;\n private handler: VideoHandler = {\n onPlay: (evt: VideoEvent) => {\n this.updatePlaybackState('playing', evt);\n },\n onPause: (evt: VideoEvent) => {\n this.updatePlaybackState('paused', evt);\n },\n onEnded: (evt: VideoEvent) => {\n this.updatePlaybackState('ended', evt);\n },\n onSeeking: () => {\n const nextState: State = {\n ...this.state,\n isSeeking: true,\n };\n this.updateState(nextState);\n },\n onSeeked: (event: VideoEvent) => {\n const nextState: State = {\n ...this.state,\n isSeeking: false,\n position: event.last_position,\n };\n this.updateState(nextState);\n },\n onError: (errorMessage: string) => {\n this.updateStateWithError(errorMessage);\n },\n onTimeUpdate: (evt: TimeUpdateEvent) => {\n this.updateTime(evt);\n },\n };\n\n constructor({ videoEl, onStateChange, vendor, isEmbedded }: VideoObserverParams) {\n this.onStateChange = onStateChange;\n if (isEmbedded) {\n this.untrack = trackEmbeddedVideo(videoEl as EmbeddedVideoPlayer, this.handler, vendor);\n } else {\n this.untrack = trackHtmlVideo(videoEl as HTMLVideoElement, this.handler, vendor);\n }\n }\n\n private stateChangeHandler(previousState: State, nextState: State) {\n try {\n this.onStateChange(previousState, nextState);\n } catch (_error) {\n // Swallow callback errors to keep observer state consistent.\n }\n }\n\n private updateStateWithError(error: string) {\n const previousState = this.state;\n const nextState: State = {\n ...previousState,\n playbackState: 'error',\n errorMessage: error,\n };\n this.updateState(nextState);\n }\n\n private updatePlaybackState(playbackState: PlaybackState, event: VideoEvent) {\n const nextState: State = {\n ...this.state,\n playbackState,\n lastEvent: event,\n position: event.last_position,\n };\n this.updateState(nextState);\n }\n\n private updateTime(event: TimeUpdateEvent) {\n const lastVideoEvent = this.state.lastEvent;\n if (!lastVideoEvent || this.state.playbackState !== 'playing') {\n return;\n }\n const isSeeking = event.isSeeking || this.state.isSeeking;\n const lastPosition = this.state.position ?? 0;\n const nextPosition = event.position;\n if (isSeeking) {\n this.state = {\n ...this.state,\n position: nextPosition,\n };\n return;\n }\n const timeDelta = nextPosition - lastPosition;\n const nextState: State = {\n ...this.state,\n position: nextPosition,\n watchTime: (this.state.watchTime ?? 0) + timeDelta,\n };\n this.updateState(nextState);\n }\n\n private updateState(nextState: State) {\n const previousState = this.state;\n this.state = nextState;\n this.stateChangeHandler(previousState, nextState);\n }\n\n destroy() {\n this.untrack();\n }\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session storage key shared by page view tracking and autocapture.
|
|
3
|
+
* `pageViewId` is the legacy single-id field. `byApiKey` keeps one id per project
|
|
4
|
+
* so a second SDK cannot replace another project's id.
|
|
5
|
+
*/
|
|
6
|
+
export declare const PAGE_VIEW_SESSION_STORAGE_KEY = "AMP_PAGE_VIEW";
|
|
7
|
+
/**
|
|
8
|
+
* Returns the page view id for this project and document URL.
|
|
9
|
+
* Another SDK initialized with the same API key reuses the id for the current
|
|
10
|
+
* page instead of overwriting the value autocapture later reads.
|
|
11
|
+
*/
|
|
12
|
+
export declare const getOrCreatePageViewId: (apiKey: string | undefined, href: string) => string | undefined;
|
|
13
|
+
/** Reads the current page view id for a project, from memory or session storage. */
|
|
14
|
+
export declare const getCurrentPageViewId: (apiKey?: string) => string | undefined;
|
|
15
|
+
/** Drops in-memory page view ids. Session storage is left unchanged. */
|
|
16
|
+
export declare const clearPageViewState: () => void;
|
|
17
|
+
//# sourceMappingURL=page-view-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page-view-id.d.ts","sourceRoot":"","sources":["../../src/page-view-id.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,kBAAkB,CAAC;AA8E7D;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,WAAY,MAAM,GAAG,SAAS,QAAQ,MAAM,KAAG,MAAM,GAAG,SAiBzF,CAAC;AAeF,oFAAoF;AACpF,eAAO,MAAM,oBAAoB,YAAa,MAAM,KAAG,MAAM,GAAG,SAM/D,CAAC;AAEF,wEAAwE;AACxE,eAAO,MAAM,kBAAkB,QAAO,IAKrC,CAAC"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clearPageViewState = exports.getCurrentPageViewId = exports.getOrCreatePageViewId = exports.PAGE_VIEW_SESSION_STORAGE_KEY = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var global_scope_1 = require("./global-scope");
|
|
6
|
+
var uuid_1 = require("./utils/uuid");
|
|
7
|
+
/**
|
|
8
|
+
* Session storage key shared by page view tracking and autocapture.
|
|
9
|
+
* `pageViewId` is the legacy single-id field. `byApiKey` keeps one id per project
|
|
10
|
+
* so a second SDK cannot replace another project's id.
|
|
11
|
+
*/
|
|
12
|
+
exports.PAGE_VIEW_SESSION_STORAGE_KEY = 'AMP_PAGE_VIEW';
|
|
13
|
+
var PAGE_VIEW_STATE = Symbol.for('@amplitude/page-view-state');
|
|
14
|
+
var storageKeyFor = function (apiKey) { return apiKey || ''; };
|
|
15
|
+
var getScope = function () { return (0, global_scope_1.getGlobalScope)(); };
|
|
16
|
+
var readState = function () { var _a; return (_a = getScope()) === null || _a === void 0 ? void 0 : _a[PAGE_VIEW_STATE]; };
|
|
17
|
+
var readStored = function (scope) {
|
|
18
|
+
var _a;
|
|
19
|
+
if (scope === void 0) { scope = getScope(); }
|
|
20
|
+
try {
|
|
21
|
+
var raw = (_a = scope === null || scope === void 0 ? void 0 : scope.sessionStorage) === null || _a === void 0 ? void 0 : _a.getItem(exports.PAGE_VIEW_SESSION_STORAGE_KEY);
|
|
22
|
+
if (!raw) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
var parsed = JSON.parse(raw);
|
|
26
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return parsed;
|
|
30
|
+
}
|
|
31
|
+
catch (_b) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var copyStoredIds = function (byApiKey) {
|
|
36
|
+
var e_1, _a;
|
|
37
|
+
var ids = {};
|
|
38
|
+
if (!byApiKey || typeof byApiKey !== 'object') {
|
|
39
|
+
return ids;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
for (var _b = tslib_1.__values(Object.entries(byApiKey)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
43
|
+
var _d = tslib_1.__read(_c.value, 2), key = _d[0], value = _d[1];
|
|
44
|
+
if (typeof value === 'string') {
|
|
45
|
+
ids[key] = value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
50
|
+
finally {
|
|
51
|
+
try {
|
|
52
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
53
|
+
}
|
|
54
|
+
finally { if (e_1) throw e_1.error; }
|
|
55
|
+
}
|
|
56
|
+
return ids;
|
|
57
|
+
};
|
|
58
|
+
var persistPageViewId = function (scope, apiKey, pageViewId) {
|
|
59
|
+
var _a;
|
|
60
|
+
try {
|
|
61
|
+
var storage = scope.sessionStorage;
|
|
62
|
+
if (!storage) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
var byApiKey = copyStoredIds((_a = readStored(scope)) === null || _a === void 0 ? void 0 : _a.byApiKey);
|
|
66
|
+
var key = storageKeyFor(apiKey);
|
|
67
|
+
if (key) {
|
|
68
|
+
byApiKey[key] = pageViewId;
|
|
69
|
+
}
|
|
70
|
+
storage.setItem(exports.PAGE_VIEW_SESSION_STORAGE_KEY, JSON.stringify(tslib_1.__assign({ pageViewId: pageViewId }, (Object.keys(byApiKey).length > 0 ? { byApiKey: byApiKey } : {}))));
|
|
71
|
+
}
|
|
72
|
+
catch (_b) {
|
|
73
|
+
// sessionStorage can throw in sandboxed documents
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Returns the page view id for this project and document URL.
|
|
78
|
+
* Another SDK initialized with the same API key reuses the id for the current
|
|
79
|
+
* page instead of overwriting the value autocapture later reads.
|
|
80
|
+
*/
|
|
81
|
+
var getOrCreatePageViewId = function (apiKey, href) {
|
|
82
|
+
var _a;
|
|
83
|
+
var scope = getScope();
|
|
84
|
+
if (!scope) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
var key = storageKeyFor(apiKey);
|
|
88
|
+
var state = (_a = scope[PAGE_VIEW_STATE]) !== null && _a !== void 0 ? _a : { byApiKey: {} };
|
|
89
|
+
var existing = state.byApiKey[key];
|
|
90
|
+
if (existing && existing.href === href) {
|
|
91
|
+
persistPageViewId(scope, apiKey, existing.pageViewId);
|
|
92
|
+
return existing.pageViewId;
|
|
93
|
+
}
|
|
94
|
+
var pageViewId = (0, uuid_1.UUID)();
|
|
95
|
+
state.byApiKey[key] = { pageViewId: pageViewId, href: href };
|
|
96
|
+
scope[PAGE_VIEW_STATE] = state;
|
|
97
|
+
persistPageViewId(scope, apiKey, pageViewId);
|
|
98
|
+
return pageViewId;
|
|
99
|
+
};
|
|
100
|
+
exports.getOrCreatePageViewId = getOrCreatePageViewId;
|
|
101
|
+
var readScopedStoredId = function (apiKey) {
|
|
102
|
+
var stored = readStored();
|
|
103
|
+
if (!stored) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
var key = storageKeyFor(apiKey);
|
|
107
|
+
if (key && stored.byApiKey && typeof stored.byApiKey === 'object') {
|
|
108
|
+
var scoped = stored.byApiKey[key];
|
|
109
|
+
return typeof scoped === 'string' ? scoped : undefined;
|
|
110
|
+
}
|
|
111
|
+
return typeof stored.pageViewId === 'string' ? stored.pageViewId : undefined;
|
|
112
|
+
};
|
|
113
|
+
/** Reads the current page view id for a project, from memory or session storage. */
|
|
114
|
+
var getCurrentPageViewId = function (apiKey) {
|
|
115
|
+
var _a, _b;
|
|
116
|
+
var inMemory = (_b = (_a = readState()) === null || _a === void 0 ? void 0 : _a.byApiKey[storageKeyFor(apiKey)]) === null || _b === void 0 ? void 0 : _b.pageViewId;
|
|
117
|
+
if (inMemory) {
|
|
118
|
+
return inMemory;
|
|
119
|
+
}
|
|
120
|
+
return readScopedStoredId(apiKey);
|
|
121
|
+
};
|
|
122
|
+
exports.getCurrentPageViewId = getCurrentPageViewId;
|
|
123
|
+
/** Drops in-memory page view ids. Session storage is left unchanged. */
|
|
124
|
+
var clearPageViewState = function () {
|
|
125
|
+
var scope = getScope();
|
|
126
|
+
if (scope) {
|
|
127
|
+
delete scope[PAGE_VIEW_STATE];
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
exports.clearPageViewState = clearPageViewState;
|
|
131
|
+
//# sourceMappingURL=page-view-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page-view-id.js","sourceRoot":"","sources":["../../src/page-view-id.ts"],"names":[],"mappings":";;;;AAAA,+CAAgD;AAChD,qCAAoC;AAEpC;;;;GAIG;AACU,QAAA,6BAA6B,GAAG,eAAe,CAAC;AAE7D,IAAM,eAAe,GAAG,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;AAkBjE,IAAM,aAAa,GAAG,UAAC,MAA0B,IAAa,OAAA,MAAM,IAAI,EAAE,EAAZ,CAAY,CAAC;AAE3E,IAAM,QAAQ,GAAG,cAAiC,OAAA,IAAA,6BAAc,GAA+B,EAA7C,CAA6C,CAAC;AAEhG,IAAM,SAAS,GAAG,sBAAiC,OAAA,MAAA,QAAQ,EAAE,0CAAG,eAAe,CAAC,CAAA,EAAA,CAAC;AAEjF,IAAM,UAAU,GAAG,UAAC,KAA6C;;IAA7C,sBAAA,EAAA,QAAmC,QAAQ,EAAE;IAC/D,IAAI;QACF,IAAM,GAAG,GAAG,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,cAAc,0CAAE,OAAO,CAAC,qCAA6B,CAAC,CAAC;QAC1E,IAAI,CAAC,GAAG,EAAE;YACR,OAAO,SAAS,CAAC;SAClB;QACD,IAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;YACzC,OAAO,SAAS,CAAC;SAClB;QACD,OAAO,MAAyB,CAAC;KAClC;IAAC,WAAM;QACN,OAAO,SAAS,CAAC;KAClB;AACH,CAAC,CAAC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AAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;KACxD;IACD,OAAO,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/E,CAAC,CAAC;AAEF,oFAAoF;AAC7E,IAAM,oBAAoB,GAAG,UAAC,MAAe;;IAClD,IAAM,QAAQ,GAAG,MAAA,MAAA,SAAS,EAAE,0CAAE,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,0CAAE,UAAU,CAAC;IAC1E,IAAI,QAAQ,EAAE;QACZ,OAAO,QAAQ,CAAC;KACjB;IACD,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC;AACpC,CAAC,CAAC;AANW,QAAA,oBAAoB,wBAM/B;AAEF,wEAAwE;AACjE,IAAM,kBAAkB,GAAG;IAChC,IAAM,KAAK,GAAG,QAAQ,EAAE,CAAC;IACzB,IAAI,KAAK,EAAE;QACT,OAAO,KAAK,CAAC,eAAe,CAAC,CAAC;KAC/B;AACH,CAAC,CAAC;AALW,QAAA,kBAAkB,sBAK7B","sourcesContent":["import { getGlobalScope } from './global-scope';\nimport { UUID } from './utils/uuid';\n\n/**\n * Session storage key shared by page view tracking and autocapture.\n * `pageViewId` is the legacy single-id field. `byApiKey` keeps one id per project\n * so a second SDK cannot replace another project's id.\n */\nexport const PAGE_VIEW_SESSION_STORAGE_KEY = 'AMP_PAGE_VIEW';\n\nconst PAGE_VIEW_STATE = Symbol.for('@amplitude/page-view-state');\n\ninterface PageViewEntry {\n pageViewId: string;\n href: string;\n}\n\ninterface PageViewState {\n byApiKey: Record<string, PageViewEntry>;\n}\n\ninterface StoredPageViews {\n pageViewId?: unknown;\n byApiKey?: unknown;\n}\n\ntype PageViewScope = NonNullable<ReturnType<typeof getGlobalScope>> & Record<symbol, PageViewState | undefined>;\n\nconst storageKeyFor = (apiKey: string | undefined): string => apiKey || '';\n\nconst getScope = (): PageViewScope | undefined => getGlobalScope() as PageViewScope | undefined;\n\nconst readState = (): PageViewState | undefined => getScope()?.[PAGE_VIEW_STATE];\n\nconst readStored = (scope: PageViewScope | undefined = getScope()): StoredPageViews | undefined => {\n try {\n const raw = scope?.sessionStorage?.getItem(PAGE_VIEW_SESSION_STORAGE_KEY);\n if (!raw) {\n return undefined;\n }\n const parsed: unknown = JSON.parse(raw);\n if (!parsed || typeof parsed !== 'object') {\n return undefined;\n }\n return parsed as StoredPageViews;\n } catch {\n return undefined;\n }\n};\n\nconst copyStoredIds = (byApiKey: unknown): Record<string, string> => {\n const ids: Record<string, string> = {};\n if (!byApiKey || typeof byApiKey !== 'object') {\n return ids;\n }\n for (const [key, value] of Object.entries(byApiKey as Record<string, unknown>)) {\n if (typeof value === 'string') {\n ids[key] = value;\n }\n }\n return ids;\n};\n\nconst persistPageViewId = (scope: PageViewScope, apiKey: string | undefined, pageViewId: string): void => {\n try {\n const storage = scope.sessionStorage;\n if (!storage) {\n return;\n }\n const byApiKey = copyStoredIds(readStored(scope)?.byApiKey);\n const key = storageKeyFor(apiKey);\n if (key) {\n byApiKey[key] = pageViewId;\n }\n storage.setItem(\n PAGE_VIEW_SESSION_STORAGE_KEY,\n JSON.stringify({\n pageViewId,\n ...(Object.keys(byApiKey).length > 0 ? { byApiKey } : {}),\n }),\n );\n } catch {\n // sessionStorage can throw in sandboxed documents\n }\n};\n\n/**\n * Returns the page view id for this project and document URL.\n * Another SDK initialized with the same API key reuses the id for the current\n * page instead of overwriting the value autocapture later reads.\n */\nexport const getOrCreatePageViewId = (apiKey: string | undefined, href: string): string | undefined => {\n const scope = getScope();\n if (!scope) {\n return undefined;\n }\n const key = storageKeyFor(apiKey);\n const state = scope[PAGE_VIEW_STATE] ?? { byApiKey: {} };\n const existing = state.byApiKey[key];\n if (existing && existing.href === href) {\n persistPageViewId(scope, apiKey, existing.pageViewId);\n return existing.pageViewId;\n }\n const pageViewId = UUID();\n state.byApiKey[key] = { pageViewId, href };\n scope[PAGE_VIEW_STATE] = state;\n persistPageViewId(scope, apiKey, pageViewId);\n return pageViewId;\n};\n\nconst readScopedStoredId = (apiKey: string | undefined): string | undefined => {\n const stored = readStored();\n if (!stored) {\n return undefined;\n }\n const key = storageKeyFor(apiKey);\n if (key && stored.byApiKey && typeof stored.byApiKey === 'object') {\n const scoped = (stored.byApiKey as Record<string, unknown>)[key];\n return typeof scoped === 'string' ? scoped : undefined;\n }\n return typeof stored.pageViewId === 'string' ? stored.pageViewId : undefined;\n};\n\n/** Reads the current page view id for a project, from memory or session storage. */\nexport const getCurrentPageViewId = (apiKey?: string): string | undefined => {\n const inMemory = readState()?.byApiKey[storageKeyFor(apiKey)]?.pageViewId;\n if (inMemory) {\n return inMemory;\n }\n return readScopedStoredId(apiKey);\n};\n\n/** Drops in-memory page view ids. Session storage is left unchanged. */\nexport const clearPageViewState = (): void => {\n const scope = getScope();\n if (scope) {\n delete scope[PAGE_VIEW_STATE];\n }\n};\n"]}
|
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
import { DestinationPlugin } from '../types/plugin';
|
|
2
2
|
import { Event } from '../types/event/event';
|
|
3
|
-
import { Delay } from '../types/event/base-event';
|
|
4
3
|
import { Result } from '../types/result';
|
|
5
4
|
import { Response, InvalidResponse, PayloadTooLargeResponse, RateLimitResponse, SuccessResponse } from '../types/response';
|
|
6
5
|
import { IConfig } from '../types/config/core-config';
|
|
7
6
|
import { EventCallback } from '../types/event-callback';
|
|
8
7
|
import { IDiagnosticsClient } from '../diagnostics/diagnostics-client';
|
|
9
|
-
import { DelayedPayload, Payload } from '../types/payload';
|
|
10
8
|
export interface Context {
|
|
11
9
|
event: Event;
|
|
12
10
|
attempts: number;
|
|
13
11
|
callback: EventCallback;
|
|
14
12
|
timeout: number;
|
|
15
|
-
delay?: Delay;
|
|
16
13
|
}
|
|
17
|
-
type DelayedEventsById = Record<string, Context[]>;
|
|
18
14
|
export declare function getResponseBodyString(res: Response): string;
|
|
19
15
|
export declare class Destination implements DestinationPlugin {
|
|
20
16
|
name: string;
|
|
@@ -28,32 +24,17 @@ export declare class Destination implements DestinationPlugin {
|
|
|
28
24
|
flushId: ReturnType<typeof setTimeout> | null;
|
|
29
25
|
queue: Context[];
|
|
30
26
|
diagnosticsClient: IDiagnosticsClient | undefined;
|
|
31
|
-
inFlightDelayedEvents: Record<string, boolean>;
|
|
32
27
|
constructor(context?: {
|
|
33
28
|
diagnosticsClient: IDiagnosticsClient;
|
|
34
29
|
});
|
|
35
30
|
setup(config: IConfig): Promise<undefined>;
|
|
36
31
|
execute(event: Event): Promise<Result>;
|
|
37
|
-
/**
|
|
38
|
-
* If a stale delayed event is sitting in the queue, and it is not in flight,
|
|
39
|
-
* remove it and resolve as "stale" with status code 0.
|
|
40
|
-
*
|
|
41
|
-
* If a matching delayed event is already in flight, clone `delay` onto the
|
|
42
|
-
* incoming event and set skipRemoval so the in-flight send does not drop the
|
|
43
|
-
* replacement. Earlier replacements waiting on the same send are discarded
|
|
44
|
-
* as stale so only the latest copy remains.
|
|
45
|
-
* @param incomingEvent { Event } the new event to check old events against
|
|
46
|
-
* @returns void
|
|
47
|
-
*/
|
|
48
|
-
private removeStaleDelayedEvents;
|
|
49
32
|
removeEventsExceedFlushMaxRetries(list: Context[]): Context[];
|
|
50
33
|
scheduleEvents(list: Context[]): void;
|
|
51
34
|
schedule(timeout: number): void;
|
|
52
35
|
resetSchedule(): void;
|
|
53
36
|
flush(useRetry?: boolean): Promise<void>;
|
|
54
|
-
|
|
55
|
-
translatePayloadToDelayedPayload(payload: Payload & Partial<DelayedPayload>, list: Context[]): void;
|
|
56
|
-
send(list: Context[], useRetry?: boolean, delay?: boolean): Promise<void>;
|
|
37
|
+
send(list: Context[], useRetry?: boolean): Promise<void>;
|
|
57
38
|
handleResponse(res: Response, list: Context[]): void;
|
|
58
39
|
handleSuccessResponse(res: SuccessResponse, list: Context[]): void;
|
|
59
40
|
handleInvalidResponse(res: InvalidResponse, list: Context[]): void;
|
|
@@ -74,5 +55,4 @@ export declare class Destination implements DestinationPlugin {
|
|
|
74
55
|
*/
|
|
75
56
|
removeEvents(eventsToRemove: Context[]): void;
|
|
76
57
|
}
|
|
77
|
-
export {};
|
|
78
58
|
//# sourceMappingURL=destination.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"destination.d.ts","sourceRoot":"","sources":["../../../src/plugins/destination.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,
|
|
1
|
+
{"version":3,"file":"destination.d.ts","sourceRoot":"","sources":["../../../src/plugins/destination.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EACL,QAAQ,EACR,eAAe,EACf,uBAAuB,EACvB,iBAAiB,EACjB,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAmB3B,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AAIvE,MAAM,WAAW,OAAO;IACtB,KAAK,EAAE,KAAK,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,aAAa,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;CACjB;AAsBD,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,QAAQ,UAUlD;AAED,qBAAa,WAAY,YAAW,iBAAiB;IACnD,IAAI,SAAe;IACnB,IAAI,gBAA0B;IAE9B,YAAY,SAAQ;IACpB,eAAe,SAAS;IACxB,UAAU,SAAM;IAIhB,MAAM,EAAE,OAAO,CAAC;IAKhB,UAAU,EAAE,UAAU,C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E;CAOvC"}
|
|
@@ -29,9 +29,6 @@ function getErrorMessage(error) {
|
|
|
29
29
|
return error.message;
|
|
30
30
|
return String(error);
|
|
31
31
|
}
|
|
32
|
-
function isDelayedEvent(event) {
|
|
33
|
-
return event.delay !== undefined && event.delay !== null;
|
|
34
|
-
}
|
|
35
32
|
function getResponseBodyString(res) {
|
|
36
33
|
var responseBodyString = '';
|
|
37
34
|
try {
|
|
@@ -63,7 +60,6 @@ var Destination = /** @class */ (function () {
|
|
|
63
60
|
// When flush resolves, set `flushId` to null
|
|
64
61
|
this.flushId = null;
|
|
65
62
|
this.queue = [];
|
|
66
|
-
this.inFlightDelayedEvents = {};
|
|
67
63
|
this.diagnosticsClient = context === null || context === void 0 ? void 0 : context.diagnosticsClient;
|
|
68
64
|
}
|
|
69
65
|
Destination.prototype.setup = function (config) {
|
|
@@ -100,63 +96,11 @@ var Destination = /** @class */ (function () {
|
|
|
100
96
|
callback: function (result) { return resolve(result); },
|
|
101
97
|
timeout: 0,
|
|
102
98
|
};
|
|
103
|
-
if (isDelayedEvent(event)) {
|
|
104
|
-
_this.removeStaleDelayedEvents(event);
|
|
105
|
-
}
|
|
106
99
|
_this.queue.push(context);
|
|
107
100
|
_this.schedule(_this.config.flushIntervalMillis);
|
|
108
101
|
_this.saveEvents();
|
|
109
102
|
});
|
|
110
103
|
};
|
|
111
|
-
/**
|
|
112
|
-
* If a stale delayed event is sitting in the queue, and it is not in flight,
|
|
113
|
-
* remove it and resolve as "stale" with status code 0.
|
|
114
|
-
*
|
|
115
|
-
* If a matching delayed event is already in flight, clone `delay` onto the
|
|
116
|
-
* incoming event and set skipRemoval so the in-flight send does not drop the
|
|
117
|
-
* replacement. Earlier replacements waiting on the same send are discarded
|
|
118
|
-
* as stale so only the latest copy remains.
|
|
119
|
-
* @param incomingEvent { Event } the new event to check old events against
|
|
120
|
-
* @returns void
|
|
121
|
-
*/
|
|
122
|
-
Destination.prototype.removeStaleDelayedEvents = function (incomingEvent) {
|
|
123
|
-
var _this = this;
|
|
124
|
-
try {
|
|
125
|
-
/* istanbul ignore next */
|
|
126
|
-
this.queue = this.queue.filter(function (context) {
|
|
127
|
-
var _a;
|
|
128
|
-
var targetEvent = context.event;
|
|
129
|
-
if (!isDelayedEvent(targetEvent)) {
|
|
130
|
-
return true;
|
|
131
|
-
}
|
|
132
|
-
// target event matches the incoming event if it has the same insert_id and delay id
|
|
133
|
-
// as the incoming event (the incoming event takes precedence)
|
|
134
|
-
var isMatchingEvent = targetEvent.delay.id === incomingEvent.delay.id && targetEvent.insert_id === incomingEvent.insert_id;
|
|
135
|
-
// do not filter out non matching events
|
|
136
|
-
if (!isMatchingEvent) {
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
// are there delayed events for this delay id currently in flight?
|
|
140
|
-
var areDelayedEventsInFlight = _this.inFlightDelayedEvents[incomingEvent.delay.id];
|
|
141
|
-
// if they are, then protect the target event from being removed from the queue
|
|
142
|
-
// after the in-flight send completes by setting skipRemoval on the incoming event
|
|
143
|
-
if (areDelayedEventsInFlight) {
|
|
144
|
-
incomingEvent.delay = tslib_1.__assign(tslib_1.__assign({}, incomingEvent.delay), { skipRemoval: true });
|
|
145
|
-
}
|
|
146
|
-
// if events are not in flight, or if the target event is marked to skipRemoval,
|
|
147
|
-
// then the incoming event supersedes the target event, and target event should be dropped
|
|
148
|
-
if (!areDelayedEventsInFlight || ((_a = targetEvent.delay) === null || _a === void 0 ? void 0 : _a.skipRemoval)) {
|
|
149
|
-
context.callback((0, result_builder_1.buildResult)(targetEvent, 0, 'Stale event overwritten'));
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
return true;
|
|
153
|
-
});
|
|
154
|
-
/* istanbul ignore next */
|
|
155
|
-
}
|
|
156
|
-
catch (e) {
|
|
157
|
-
// swallow error
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
104
|
Destination.prototype.removeEventsExceedFlushMaxRetries = function (list) {
|
|
161
105
|
var _this = this;
|
|
162
106
|
return list.filter(function (context) {
|
|
@@ -207,7 +151,7 @@ var Destination = /** @class */ (function () {
|
|
|
207
151
|
Destination.prototype.flush = function (useRetry) {
|
|
208
152
|
if (useRetry === void 0) { useRetry = false; }
|
|
209
153
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
210
|
-
var list,
|
|
154
|
+
var list, later, batches;
|
|
211
155
|
var _this = this;
|
|
212
156
|
return tslib_1.__generator(this, function (_a) {
|
|
213
157
|
switch (_a.label) {
|
|
@@ -226,40 +170,25 @@ var Destination = /** @class */ (function () {
|
|
|
226
170
|
this.flushId = this.scheduleId;
|
|
227
171
|
this.resetSchedule();
|
|
228
172
|
list = [];
|
|
229
|
-
delayed = {};
|
|
230
173
|
later = [];
|
|
231
|
-
this.queue.forEach(function (context) {
|
|
232
|
-
var _a;
|
|
233
|
-
if (context.timeout !== 0) {
|
|
234
|
-
later.push(context);
|
|
235
|
-
}
|
|
236
|
-
else if ((_a = context.event.delay) === null || _a === void 0 ? void 0 : _a.id) {
|
|
237
|
-
var delay = context.event.delay;
|
|
238
|
-
delayed[delay.id] = delayed[delay.id] || [];
|
|
239
|
-
delayed[delay.id].push(context);
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
list.push(context);
|
|
243
|
-
}
|
|
244
|
-
});
|
|
174
|
+
this.queue.forEach(function (context) { return (context.timeout === 0 ? list.push(context) : later.push(context)); });
|
|
245
175
|
batches = (0, chunk_1.chunk)(list, this.config.flushQueueSize);
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
eventPromises.push(this.sendDelayedEvents(delayed, useRetry));
|
|
260
|
-
}
|
|
261
|
-
return [4 /*yield*/, Promise.all(eventPromises)];
|
|
176
|
+
// Promise.all() doesn't guarantee resolve order.
|
|
177
|
+
// Sequentially resolve to make sure backend receives events in order
|
|
178
|
+
return [4 /*yield*/, batches.reduce(function (promise, batch) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
179
|
+
return tslib_1.__generator(this, function (_a) {
|
|
180
|
+
switch (_a.label) {
|
|
181
|
+
case 0: return [4 /*yield*/, promise];
|
|
182
|
+
case 1:
|
|
183
|
+
_a.sent();
|
|
184
|
+
return [4 /*yield*/, this.send(batch, useRetry)];
|
|
185
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}); }, Promise.resolve())];
|
|
262
189
|
case 1:
|
|
190
|
+
// Promise.all() doesn't guarantee resolve order.
|
|
191
|
+
// Sequentially resolve to make sure backend receives events in order
|
|
263
192
|
_a.sent();
|
|
264
193
|
// Mark current flush is done
|
|
265
194
|
this.flushId = null;
|
|
@@ -269,77 +198,11 @@ var Destination = /** @class */ (function () {
|
|
|
269
198
|
});
|
|
270
199
|
});
|
|
271
200
|
};
|
|
272
|
-
Destination.prototype.
|
|
273
|
-
var e_1, _a;
|
|
274
|
-
var _this = this;
|
|
275
|
-
var eventPromises = [];
|
|
276
|
-
try {
|
|
277
|
-
var _loop_1 = function (delayId, contexts) {
|
|
278
|
-
this_1.inFlightDelayedEvents[delayId] = true;
|
|
279
|
-
var delayedEventsSend = this_1.send(contexts, useRetry, true);
|
|
280
|
-
eventPromises.push(delayedEventsSend);
|
|
281
|
-
delayedEventsSend.finally(function () {
|
|
282
|
-
delete _this.inFlightDelayedEvents[delayId];
|
|
283
|
-
});
|
|
284
|
-
};
|
|
285
|
-
var this_1 = this;
|
|
286
|
-
try {
|
|
287
|
-
for (var _b = tslib_1.__values(Object.entries(delayed)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
288
|
-
var _d = tslib_1.__read(_c.value, 2), delayId = _d[0], contexts = _d[1];
|
|
289
|
-
_loop_1(delayId, contexts);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
293
|
-
finally {
|
|
294
|
-
try {
|
|
295
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
296
|
-
}
|
|
297
|
-
finally { if (e_1) throw e_1.error; }
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
catch (e) {
|
|
301
|
-
// swallow error
|
|
302
|
-
}
|
|
303
|
-
return eventPromises.reduce(function (promise, batch) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
304
|
-
return tslib_1.__generator(this, function (_a) {
|
|
305
|
-
switch (_a.label) {
|
|
306
|
-
case 0: return [4 /*yield*/, promise];
|
|
307
|
-
case 1:
|
|
308
|
-
_a.sent();
|
|
309
|
-
return [4 /*yield*/, batch];
|
|
310
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
}); }, Promise.resolve());
|
|
314
|
-
};
|
|
315
|
-
Destination.prototype.translatePayloadToDelayedPayload = function (payload, list) {
|
|
316
|
-
var _a, _b;
|
|
317
|
-
var delayedEvents = [];
|
|
318
|
-
var instantEvents = [];
|
|
319
|
-
var delayedContexts = [];
|
|
320
|
-
var instantContexts = [];
|
|
321
|
-
list.forEach(function (context, index) {
|
|
322
|
-
if (context.event.delay.timeout) {
|
|
323
|
-
delayedEvents.push(payload.events[index]);
|
|
324
|
-
delayedContexts.push(context);
|
|
325
|
-
}
|
|
326
|
-
else {
|
|
327
|
-
instantEvents.push(payload.events[index]);
|
|
328
|
-
instantContexts.push(context);
|
|
329
|
-
}
|
|
330
|
-
});
|
|
331
|
-
list.splice.apply(list, tslib_1.__spreadArray(tslib_1.__spreadArray([0, list.length], tslib_1.__read(delayedContexts), false), tslib_1.__read(instantContexts), false));
|
|
332
|
-
/* istanbul ignore next */
|
|
333
|
-
payload.timeout = (_b = (_a = delayedContexts[0]) === null || _a === void 0 ? void 0 : _a.event.delay.timeout) !== null && _b !== void 0 ? _b : 0;
|
|
334
|
-
payload.id = list[0].event.delay.id;
|
|
335
|
-
payload.events = delayedEvents;
|
|
336
|
-
payload.instant_events = instantEvents;
|
|
337
|
-
};
|
|
338
|
-
Destination.prototype.send = function (list, useRetry, delay) {
|
|
201
|
+
Destination.prototype.send = function (list, useRetry) {
|
|
339
202
|
var _a;
|
|
340
203
|
if (useRetry === void 0) { useRetry = true; }
|
|
341
204
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
342
|
-
var payload, serverUrl, shouldCompressUploadBody, res,
|
|
205
|
+
var payload, serverUrl, shouldCompressUploadBody, res, e_1, errorMessage;
|
|
343
206
|
return tslib_1.__generator(this, function (_b) {
|
|
344
207
|
switch (_b.label) {
|
|
345
208
|
case 0:
|
|
@@ -350,28 +213,21 @@ var Destination = /** @class */ (function () {
|
|
|
350
213
|
api_key: this.config.apiKey,
|
|
351
214
|
events: list.map(function (context) {
|
|
352
215
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
353
|
-
var _a = context.event, extra = _a.extra,
|
|
216
|
+
var _a = context.event, extra = _a.extra, eventWithoutExtra = tslib_1.__rest(_a, ["extra"]);
|
|
354
217
|
return eventWithoutExtra;
|
|
355
218
|
}),
|
|
356
219
|
options: {
|
|
357
220
|
min_id_length: this.config.minIdLength,
|
|
358
221
|
},
|
|
359
222
|
client_upload_time: new Date().toISOString(),
|
|
360
|
-
request_metadata:
|
|
223
|
+
request_metadata: this.config.requestMetadata,
|
|
361
224
|
};
|
|
362
|
-
|
|
363
|
-
this.config.requestMetadata = new config_1.RequestMetadata();
|
|
364
|
-
}
|
|
225
|
+
this.config.requestMetadata = new config_1.RequestMetadata();
|
|
365
226
|
_b.label = 1;
|
|
366
227
|
case 1:
|
|
367
228
|
_b.trys.push([1, 3, , 4]);
|
|
368
229
|
serverUrl = (0, config_1.createServerConfig)(this.config.serverUrl, this.config.serverZone, this.config.useBatch).serverUrl;
|
|
369
230
|
shouldCompressUploadBody = shouldCompressUploadBodyForRequest(serverUrl, this.config.enableRequestBodyCompression);
|
|
370
|
-
if (delay) {
|
|
371
|
-
serverUrl = this.config.delayedEventsServerUrl || "".concat(serverUrl, "/delayed");
|
|
372
|
-
this.translatePayloadToDelayedPayload(payload, list);
|
|
373
|
-
shouldCompressUploadBody = false; // delayed events doesn't support compression
|
|
374
|
-
}
|
|
375
231
|
return [4 /*yield*/, this.config.transportProvider.send(serverUrl, payload, shouldCompressUploadBody)];
|
|
376
232
|
case 2:
|
|
377
233
|
res = _b.sent();
|
|
@@ -391,8 +247,8 @@ var Destination = /** @class */ (function () {
|
|
|
391
247
|
this.handleResponse(res, list);
|
|
392
248
|
return [3 /*break*/, 4];
|
|
393
249
|
case 3:
|
|
394
|
-
|
|
395
|
-
errorMessage = getErrorMessage(
|
|
250
|
+
e_1 = _b.sent();
|
|
251
|
+
errorMessage = getErrorMessage(e_1);
|
|
396
252
|
this.config.loggerProvider.error(errorMessage);
|
|
397
253
|
(_a = this.diagnosticsClient) === null || _a === void 0 ? void 0 : _a.recordEvent('analytics.events.unsuccessful.from.catch.error', {
|
|
398
254
|
events: list.map(function (context) { return context.event.event_type; }),
|
|
@@ -550,16 +406,9 @@ var Destination = /** @class */ (function () {
|
|
|
550
406
|
* This is called on response comes back for a request
|
|
551
407
|
*/
|
|
552
408
|
Destination.prototype.removeEvents = function (eventsToRemove) {
|
|
553
|
-
var insertIdsBeingRemoved = new Set(eventsToRemove.map(function (context) { return context.event.insert_id; }));
|
|
554
409
|
// Remove only the exact fulfilled Context instances so duplicate insert_id events remain queued.
|
|
555
410
|
var toRemove = new Set(eventsToRemove);
|
|
556
|
-
this.queue = this.queue.filter(function (queuedContext) {
|
|
557
|
-
this.queue.forEach(function (context) {
|
|
558
|
-
var _a;
|
|
559
|
-
if (((_a = context.event.delay) === null || _a === void 0 ? void 0 : _a.skipRemoval) && insertIdsBeingRemoved.has(context.event.insert_id)) {
|
|
560
|
-
delete context.event.delay.skipRemoval;
|
|
561
|
-
}
|
|
562
|
-
});
|
|
411
|
+
this.queue = this.queue.filter(function (queuedContext) { return !toRemove.has(queuedContext); });
|
|
563
412
|
this.saveEvents();
|
|
564
413
|
};
|
|
565
414
|
return Destination;
|