Skip to content

Commit

Permalink
Fix config for callbacks (close #1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Dec 13, 2023
1 parent 26e826a commit 8a8d357
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
19 changes: 19 additions & 0 deletions api-docs/docs/browser-tracker/browser-tracker.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ export interface EnableAnonymousTrackingConfiguration {
stateStorageStrategy?: StateStorageStrategy;
}

// @public
export type EventBatch = GetBatch | PostBatch;

// @public (undocumented)
export type EventMethod = "post" | "get" | "beacon";

Expand All @@ -234,6 +237,9 @@ export interface FlushBufferConfiguration {
newBufferSize?: number;
}

// @public
export type GetBatch = string[];

// @public
export function newSession(trackers?: Array<string>): void;

Expand All @@ -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<string, unknown>[];

// @public
export function preservePageViewId(trackers?: Array<string>): void;

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

// @public
export type RequestFailure = {
events: EventBatch;
status?: number;
message?: string;
willRetry: boolean;
};

// @public
export interface RuleSet {
// (undocumented)
Expand Down Expand Up @@ -370,6 +387,8 @@ export type TrackerConfiguration = {
onSessionUpdateCallback?: (updatedSession: ClientSession) => void;
idService?: string;
retryFailedRequests?: boolean;
onRequestSuccess?: (data: EventBatch) => void;
onRequestFailure?: (data: RequestFailure) => void;
};

// @public
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-tracker-core",
"comment": "Fix config for callbacks",
"type": "none"
}
],
"packageName": "@snowplow/browser-tracker-core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-tracker",
"comment": "Update API docs for callbacks",
"type": "none"
}
],
"packageName": "@snowplow/browser-tracker"
}
4 changes: 3 additions & 1 deletion libraries/browser-tracker-core/src/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions libraries/browser-tracker-core/src/tracker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
8 changes: 8 additions & 0 deletions trackers/browser-tracker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import {
EventMethod,
StateStorageStrategy,
ClientSession,
RequestFailure,
EventBatch,
GetBatch,
PostBatch,
} from '@snowplow/browser-tracker-core';
import { version } from '@snowplow/tracker-core';

Expand Down Expand Up @@ -81,6 +85,10 @@ export {
EventMethod,
StateStorageStrategy,
ClientSession,
RequestFailure,
EventBatch,
GetBatch,
PostBatch,
};
export { version };
export * from './api';

0 comments on commit 8a8d357

Please sign in to comment.