From 5b983aefd5e260521385f74c35a1d69f4cc43e89 Mon Sep 17 00:00:00 2001 From: Greg Leonard <45019882+greg-el@users.noreply.github.com> Date: Wed, 13 Dec 2023 13:00:44 +0000 Subject: [PATCH] Fix config for callbacks (close #1275) --- .../browser-tracker/browser-tracker.api.md | 19 +++++++++++++++++++ ...config-for-callbacks_2023-12-13-13-14.json | 10 ++++++++++ ...config-for-callbacks_2023-12-13-14-30.json | 10 ++++++++++ .../browser-tracker-core/src/tracker/index.ts | 4 +++- .../browser-tracker-core/src/tracker/types.ts | 15 +++++++++++++++ trackers/browser-tracker/src/index.ts | 8 ++++++++ 6 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 common/changes/@snowplow/browser-tracker-core/issue-1275-fix-config-for-callbacks_2023-12-13-13-14.json create mode 100644 common/changes/@snowplow/browser-tracker/issue-1275-fix-config-for-callbacks_2023-12-13-14-30.json diff --git a/api-docs/docs/browser-tracker/browser-tracker.api.md b/api-docs/docs/browser-tracker/browser-tracker.api.md index f064fc99b..a449cbe07 100644 --- a/api-docs/docs/browser-tracker/browser-tracker.api.md +++ b/api-docs/docs/browser-tracker/browser-tracker.api.md @@ -215,6 +215,9 @@ export interface EnableAnonymousTrackingConfiguration { stateStorageStrategy?: StateStorageStrategy; } +// @public +export type EventBatch = GetBatch | PostBatch; + // @public (undocumented) export type EventMethod = "post" | "get" | "beacon"; @@ -234,6 +237,9 @@ export interface FlushBufferConfiguration { newBufferSize?: number; } +// @public +export type GetBatch = string[]; + // @public export function newSession(trackers?: Array): void; @@ -252,12 +258,23 @@ export interface PageViewEvent { // @public (undocumented) export type Platform = "web" | "mob" | "pc" | "srv" | "app" | "tv" | "cnsl" | "iot"; +// @public +export type PostBatch = Record[]; + // @public export function preservePageViewId(trackers?: Array): void; // @public export function removeGlobalContexts(contexts: Array, trackers?: Array): void; +// @public +export type RequestFailure = { + events: EventBatch; + status?: number; + message?: string; + willRetry: boolean; +}; + // @public export interface RuleSet { // (undocumented) @@ -370,6 +387,8 @@ export type TrackerConfiguration = { onSessionUpdateCallback?: (updatedSession: ClientSession) => void; idService?: string; retryFailedRequests?: boolean; + onRequestSuccess?: (data: EventBatch) => void; + onRequestFailure?: (data: RequestFailure) => void; }; // @public diff --git a/common/changes/@snowplow/browser-tracker-core/issue-1275-fix-config-for-callbacks_2023-12-13-13-14.json b/common/changes/@snowplow/browser-tracker-core/issue-1275-fix-config-for-callbacks_2023-12-13-13-14.json new file mode 100644 index 000000000..7cc7f8fa9 --- /dev/null +++ b/common/changes/@snowplow/browser-tracker-core/issue-1275-fix-config-for-callbacks_2023-12-13-13-14.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@snowplow/browser-tracker-core", + "comment": "Fix config for callbacks", + "type": "none" + } + ], + "packageName": "@snowplow/browser-tracker-core" +} \ No newline at end of file diff --git a/common/changes/@snowplow/browser-tracker/issue-1275-fix-config-for-callbacks_2023-12-13-14-30.json b/common/changes/@snowplow/browser-tracker/issue-1275-fix-config-for-callbacks_2023-12-13-14-30.json new file mode 100644 index 000000000..a4e350c5e --- /dev/null +++ b/common/changes/@snowplow/browser-tracker/issue-1275-fix-config-for-callbacks_2023-12-13-14-30.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@snowplow/browser-tracker", + "comment": "Update API docs for callbacks", + "type": "none" + } + ], + "packageName": "@snowplow/browser-tracker" +} \ No newline at end of file diff --git a/libraries/browser-tracker-core/src/tracker/index.ts b/libraries/browser-tracker-core/src/tracker/index.ts index 2183981c0..11a46685b 100755 --- a/libraries/browser-tracker-core/src/tracker/index.ts +++ b/libraries/browser-tracker-core/src/tracker/index.ts @@ -304,7 +304,9 @@ export function Tracker( trackerConfiguration.retryStatusCodes ?? [], (trackerConfiguration.dontRetryStatusCodes ?? []).concat([400, 401, 403, 410, 422]), trackerConfiguration.idService, - trackerConfiguration.retryFailedRequests + trackerConfiguration.retryFailedRequests, + trackerConfiguration.onRequestSuccess, + trackerConfiguration.onRequestFailure ), // Whether pageViewId should be regenerated after each trackPageView. Affect web_page context preservePageViewId = false, diff --git a/libraries/browser-tracker-core/src/tracker/types.ts b/libraries/browser-tracker-core/src/tracker/types.ts index 31042b902..fa2105500 100755 --- a/libraries/browser-tracker-core/src/tracker/types.ts +++ b/libraries/browser-tracker-core/src/tracker/types.ts @@ -232,6 +232,21 @@ export type TrackerConfiguration = { * @defaultValue true */ retryFailedRequests?: boolean; + /** + * a callback function to be executed whenever a request is successfully sent to the collector. + * In practice this means any request which returns a 2xx status code will trigger this callback. + * + * @param data - The event batch that was successfully sent + */ + onRequestSuccess?: (data: EventBatch) => void; + + /** + * a callback function to be executed whenever a request fails to be sent to the collector. + * This is the inverse of the onRequestSuccess callback, so any non 2xx status code will trigger this callback. + * + * @param data - The data associated with the event(s) that failed to send + */ + onRequestFailure?: (data: RequestFailure) => void; }; /** diff --git a/trackers/browser-tracker/src/index.ts b/trackers/browser-tracker/src/index.ts index 8acce2105..92b88c628 100644 --- a/trackers/browser-tracker/src/index.ts +++ b/trackers/browser-tracker/src/index.ts @@ -38,6 +38,10 @@ import { EventMethod, StateStorageStrategy, ClientSession, + RequestFailure, + EventBatch, + GetBatch, + PostBatch, } from '@snowplow/browser-tracker-core'; import { version } from '@snowplow/tracker-core'; @@ -81,6 +85,10 @@ export { EventMethod, StateStorageStrategy, ClientSession, + RequestFailure, + EventBatch, + GetBatch, + PostBatch, }; export { version }; export * from './api';