Skip to content

Commit 8a8d357

Browse files
committed
Fix config for callbacks (close #1275)
1 parent 26e826a commit 8a8d357

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

api-docs/docs/browser-tracker/browser-tracker.api.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ export interface EnableAnonymousTrackingConfiguration {
215215
stateStorageStrategy?: StateStorageStrategy;
216216
}
217217

218+
// @public
219+
export type EventBatch = GetBatch | PostBatch;
220+
218221
// @public (undocumented)
219222
export type EventMethod = "post" | "get" | "beacon";
220223

@@ -234,6 +237,9 @@ export interface FlushBufferConfiguration {
234237
newBufferSize?: number;
235238
}
236239

240+
// @public
241+
export type GetBatch = string[];
242+
237243
// @public
238244
export function newSession(trackers?: Array<string>): void;
239245

@@ -252,12 +258,23 @@ export interface PageViewEvent {
252258
// @public (undocumented)
253259
export type Platform = "web" | "mob" | "pc" | "srv" | "app" | "tv" | "cnsl" | "iot";
254260

261+
// @public
262+
export type PostBatch = Record<string, unknown>[];
263+
255264
// @public
256265
export function preservePageViewId(trackers?: Array<string>): void;
257266

258267
// @public
259268
export function removeGlobalContexts(contexts: Array<ConditionalContextProvider | ContextPrimitive>, trackers?: Array<string>): void;
260269

270+
// @public
271+
export type RequestFailure = {
272+
events: EventBatch;
273+
status?: number;
274+
message?: string;
275+
willRetry: boolean;
276+
};
277+
261278
// @public
262279
export interface RuleSet {
263280
// (undocumented)
@@ -370,6 +387,8 @@ export type TrackerConfiguration = {
370387
onSessionUpdateCallback?: (updatedSession: ClientSession) => void;
371388
idService?: string;
372389
retryFailedRequests?: boolean;
390+
onRequestSuccess?: (data: EventBatch) => void;
391+
onRequestFailure?: (data: RequestFailure) => void;
373392
};
374393

375394
// @public
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker-core",
5+
"comment": "Fix config for callbacks",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker",
5+
"comment": "Update API docs for callbacks",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker"
10+
}

libraries/browser-tracker-core/src/tracker/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ export function Tracker(
304304
trackerConfiguration.retryStatusCodes ?? [],
305305
(trackerConfiguration.dontRetryStatusCodes ?? []).concat([400, 401, 403, 410, 422]),
306306
trackerConfiguration.idService,
307-
trackerConfiguration.retryFailedRequests
307+
trackerConfiguration.retryFailedRequests,
308+
trackerConfiguration.onRequestSuccess,
309+
trackerConfiguration.onRequestFailure
308310
),
309311
// Whether pageViewId should be regenerated after each trackPageView. Affect web_page context
310312
preservePageViewId = false,

libraries/browser-tracker-core/src/tracker/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ export type TrackerConfiguration = {
232232
* @defaultValue true
233233
*/
234234
retryFailedRequests?: boolean;
235+
/**
236+
* a callback function to be executed whenever a request is successfully sent to the collector.
237+
* In practice this means any request which returns a 2xx status code will trigger this callback.
238+
*
239+
* @param data - The event batch that was successfully sent
240+
*/
241+
onRequestSuccess?: (data: EventBatch) => void;
242+
243+
/**
244+
* a callback function to be executed whenever a request fails to be sent to the collector.
245+
* This is the inverse of the onRequestSuccess callback, so any non 2xx status code will trigger this callback.
246+
*
247+
* @param data - The data associated with the event(s) that failed to send
248+
*/
249+
onRequestFailure?: (data: RequestFailure) => void;
235250
};
236251

237252
/**

trackers/browser-tracker/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ import {
3838
EventMethod,
3939
StateStorageStrategy,
4040
ClientSession,
41+
RequestFailure,
42+
EventBatch,
43+
GetBatch,
44+
PostBatch,
4145
} from '@snowplow/browser-tracker-core';
4246
import { version } from '@snowplow/tracker-core';
4347

@@ -81,6 +85,10 @@ export {
8185
EventMethod,
8286
StateStorageStrategy,
8387
ClientSession,
88+
RequestFailure,
89+
EventBatch,
90+
GetBatch,
91+
PostBatch,
8492
};
8593
export { version };
8694
export * from './api';

0 commit comments

Comments
 (0)