Skip to content

Commit

Permalink
Add the idService option to the tracker configuration (close #1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
igneel64 committed May 15, 2023
1 parent 748a7cc commit b43a776
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-tracker-core",
"comment": "Add idService option",
"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": "Add idService option",
"type": "none"
}
],
"packageName": "@snowplow/browser-tracker"
}
3 changes: 2 additions & 1 deletion libraries/browser-tracker-core/src/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ export function Tracker(
trackerConfiguration.customHeaders ?? {},
trackerConfiguration.withCredentials ?? true,
trackerConfiguration.retryStatusCodes ?? [],
(trackerConfiguration.dontRetryStatusCodes ?? []).concat([400, 401, 403, 410, 422])
(trackerConfiguration.dontRetryStatusCodes ?? []).concat([400, 401, 403, 410, 422]),
trackerConfiguration.idService
),
// Whether pageViewId should be regenerated after each trackPageView. Affect web_page context
preservePageViewId = false,
Expand Down
23 changes: 19 additions & 4 deletions libraries/browser-tracker-core/src/tracker/out_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface OutQueue {
* @param withCredentials - Sets the value of the withCredentials flag on XMLHttpRequest (GET and POST) requests
* @param retryStatusCodes – Failure HTTP response status codes from Collector for which sending events should be retried (they can override the `dontRetryStatusCodes`)
* @param dontRetryStatusCodes – Failure HTTP response status codes from Collector for which sending events should not be retried
* @param idService - Id service full URL. This URL will be added to the queue and will be called using a GET method.
* @returns object OutQueueManager instance
*/
export function OutQueueManager(
Expand All @@ -81,7 +82,8 @@ export function OutQueueManager(
customHeaders: Record<string, string>,
withCredentials: boolean,
retryStatusCodes: number[],
dontRetryStatusCodes: number[]
dontRetryStatusCodes: number[],
idService?: string
): OutQueue {
type PostEvent = {
evt: Record<string, unknown>;
Expand All @@ -90,7 +92,8 @@ export function OutQueueManager(

let executingQueue = false,
configCollectorUrl: string,
outQueue: Array<PostEvent> | Array<string> = [];
outQueue: Array<PostEvent> | Array<string> = [],
idServicalled = false;

//Force to lower case if its a string
eventMethod = typeof eventMethod === 'string' ? eventMethod.toLowerCase() : eventMethod;
Expand Down Expand Up @@ -222,7 +225,7 @@ export function OutQueueManager(
}

const postable = (queue: Array<PostEvent> | Array<string>): queue is Array<PostEvent> => {
return typeof queue[0] === 'object';
return typeof queue[0] === 'object' && 'evt' in queue[0];
};

/**
Expand Down Expand Up @@ -293,7 +296,7 @@ export function OutQueueManager(
outQueue.shift();
}

if (outQueue.length < 1) {
if (!outQueue.length) {
executingQueue = false;
return;
}
Expand All @@ -305,6 +308,18 @@ export function OutQueueManager(

executingQueue = true;

if (idService && !idServicalled) {
const xhr = initializeXMLHttpRequest(idService, false, sync);
idServicalled = true;
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status >= 200) {
executeQueue();
}
};
return;
}

if (useXhr) {
// Keep track of number of events to delete from queue
const chooseHowManyToSend = (queue: Array<{ bytes: number }>) => {
Expand Down
7 changes: 7 additions & 0 deletions libraries/browser-tracker-core/src/tracker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ export type TrackerConfiguration = {
* By default, the tracker retries on all non-success status codes except for 400, 401, 403, 410, and 422.
*/
dontRetryStatusCodes?: number[];
/**
* Id service full URL. This URL will be added to the queue and will be called using a GET method.
* This option is there to allow the service URL to be called in order to set any required identifiers e.g. extra cookies.
*
* The request respects the `anonymousTracking` option, including the SP-Anonymous header if needed, and any additional custom headers from the customHeaders option.
*/
idService?: string;
};

/**
Expand Down
1 change: 1 addition & 0 deletions trackers/browser-tracker/docs/browser-tracker.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ export type TrackerConfiguration = {
customHeaders?: Record<string, string>;
retryStatusCodes?: number[];
dontRetryStatusCodes?: number[];
idService?: string;
};

// @public
Expand Down

0 comments on commit b43a776

Please sign in to comment.