diff --git a/common/changes/@snowplow/browser-tracker-core/feature-1185-id-service-option_2023-05-11-20-22.json b/common/changes/@snowplow/browser-tracker-core/feature-1185-id-service-option_2023-05-11-20-22.json new file mode 100644 index 000000000..de331a7c7 --- /dev/null +++ b/common/changes/@snowplow/browser-tracker-core/feature-1185-id-service-option_2023-05-11-20-22.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@snowplow/browser-tracker-core", + "comment": "Add idService option", + "type": "none" + } + ], + "packageName": "@snowplow/browser-tracker-core" +} \ No newline at end of file diff --git a/common/changes/@snowplow/browser-tracker/feature-1185-id-service-option_2023-05-12-07-26.json b/common/changes/@snowplow/browser-tracker/feature-1185-id-service-option_2023-05-12-07-26.json new file mode 100644 index 000000000..934020399 --- /dev/null +++ b/common/changes/@snowplow/browser-tracker/feature-1185-id-service-option_2023-05-12-07-26.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@snowplow/browser-tracker", + "comment": "Add idService option", + "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 8669b9375..61efb6e46 100755 --- a/libraries/browser-tracker-core/src/tracker/index.ts +++ b/libraries/browser-tracker-core/src/tracker/index.ts @@ -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, diff --git a/libraries/browser-tracker-core/src/tracker/out_queue.ts b/libraries/browser-tracker-core/src/tracker/out_queue.ts index b838ba436..3efb128ae 100644 --- a/libraries/browser-tracker-core/src/tracker/out_queue.ts +++ b/libraries/browser-tracker-core/src/tracker/out_queue.ts @@ -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( @@ -81,16 +82,21 @@ export function OutQueueManager( customHeaders: Record, withCredentials: boolean, retryStatusCodes: number[], - dontRetryStatusCodes: number[] + dontRetryStatusCodes: number[], + idService?: string ): OutQueue { type PostEvent = { evt: Record; bytes: number; }; + type IdServiceCall = { + url: string; + }; + let executingQueue = false, configCollectorUrl: string, - outQueue: Array | Array = []; + outQueue: Array = []; //Force to lower case if its a string eventMethod = typeof eventMethod === 'string' ? eventMethod.toLowerCase() : eventMethod; @@ -147,6 +153,10 @@ export function OutQueueManager( }); } + if (idService) { + outQueue.push({ url: idService }); + } + /* * Convert a dictionary to a querystring * The context field is the last in the querystring @@ -221,8 +231,8 @@ export function OutQueueManager( return len; } - const postable = (queue: Array | Array): queue is Array => { - return typeof queue[0] === 'object'; + const postable = (queue: Array): queue is Array => { + return typeof queue[0] === 'object' && 'evt' in queue[0]; }; /** @@ -293,7 +303,7 @@ export function OutQueueManager( outQueue.shift(); } - if (outQueue.length < 1) { + if (!outQueue.length) { executingQueue = false; return; } @@ -322,12 +332,16 @@ export function OutQueueManager( }; let url: string, xhr: XMLHttpRequest, numberToSend: number; - if (postable(outQueue)) { + if (instanceOfIdServiceCall(outQueue[0])) { + url = outQueue[0].url; + xhr = initializeXMLHttpRequest(outQueue[0].url, false, sync); + numberToSend = 1; + } else if (postable(outQueue)) { url = configCollectorUrl; xhr = initializeXMLHttpRequest(url, true, sync); numberToSend = chooseHowManyToSend(outQueue); } else { - url = createGetUrl(outQueue[0]); + url = createGetUrl(outQueue[0] as string); xhr = initializeXMLHttpRequest(url, false, sync); numberToSend = 1; } @@ -423,7 +437,7 @@ export function OutQueueManager( executingQueue = false; }; - image.src = createGetUrl(outQueue[0]); + image.src = createGetUrl(outQueue[0] as string); setTimeout(function () { if (loading && executingQueue) { @@ -567,4 +581,8 @@ export function OutQueueManager( return useragent.match('Chrom(e|ium)'); } } + + function instanceOfIdServiceCall(object: any): object is IdServiceCall { + return typeof object === 'object' && 'url' in object; + } } diff --git a/libraries/browser-tracker-core/src/tracker/types.ts b/libraries/browser-tracker-core/src/tracker/types.ts index 45751c533..83ee1236f 100755 --- a/libraries/browser-tracker-core/src/tracker/types.ts +++ b/libraries/browser-tracker-core/src/tracker/types.ts @@ -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; }; /** diff --git a/trackers/browser-tracker/docs/browser-tracker.api.md b/trackers/browser-tracker/docs/browser-tracker.api.md index 2a1762eb1..f4e27006e 100644 --- a/trackers/browser-tracker/docs/browser-tracker.api.md +++ b/trackers/browser-tracker/docs/browser-tracker.api.md @@ -347,6 +347,7 @@ export type TrackerConfiguration = { customHeaders?: Record; retryStatusCodes?: number[]; dontRetryStatusCodes?: number[]; + idService?: string; }; // @public