Skip to content

Commit

Permalink
BrowserTracker: fix method type definitions (close #1259) (#1284)
Browse files Browse the repository at this point in the history
* BrowserTracker: fix method type definitions

Per the [TypeScript
docs](https://www.typescriptlang.org/docs/handbook/2/functions.html#return-type-void),
functions that return values are type-compatible with functions declared
as having void return type; however if you're consuming these type
definitions this becomes annoying as you are required to cast `as
unknown as T` at the call sites to get the correct types actually
returned from these methods.
  • Loading branch information
jethron authored Jan 30, 2024
1 parent ffb1994 commit 71a1108
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 26 deletions.
25 changes: 20 additions & 5 deletions api-docs/docs/browser-tracker/browser-tracker.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export interface BrowserTracker {
enableActivityTrackingCallback: (configuration: ActivityTrackingConfiguration & ActivityTrackingConfigurationCallback) => void;
enableAnonymousTracking: (configuration?: EnableAnonymousTrackingConfiguration) => void;
flushBuffer: (configuration?: FlushBufferConfiguration) => void;
getCookieName: (basename: string) => void;
getDomainSessionIndex: () => void;
getDomainUserId: () => void;
getDomainUserInfo: () => void;
getCookieName: (basename: string) => string;
getDomainSessionIndex: () => number;
getDomainUserId: () => string;
getDomainUserInfo: () => ParsedIdCookie;
getPageViewId: () => string;
getTabId: () => string | null;
getUserId: () => void;
getUserId: () => string | null | undefined;
id: string;
namespace: string;
newSession: () => void;
Expand Down Expand Up @@ -267,6 +267,21 @@ export interface PageViewEvent {
title?: string | null;
}

// @public
export type ParsedIdCookie = [
cookieDisabled: string,
domainUserId: string,
cookieCreateTs: number,
visitCount: number,
nowTs: number,
lastVisitTs: number | undefined,
sessionId: string,
previousSessionId: string,
firstEventId: string,
firstEventTs: number | undefined,
eventIndex: number
];

// @public (undocumented)
export type Platform = "web" | "mob" | "pc" | "srv" | "app" | "tv" | "cnsl" | "iot";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-tracker-core",
"comment": "Update method signatures for BrowserTracker",
"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-core",
"comment": "",
"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": "",
"type": "none"
}
],
"packageName": "@snowplow/browser-tracker"
}
16 changes: 1 addition & 15 deletions libraries/browser-tracker-core/src/tracker/id_cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import { PayloadBuilder } from '@snowplow/tracker-core';
import { v4 as uuid } from 'uuid';
import { ClientSession } from './types';
import { ClientSession, ParsedIdCookie } from './types';

/**
* Indices of cookie values
Expand All @@ -47,20 +47,6 @@ const cookieDisabledIndex = 0,
firstEventTsInMsIndex = 9,
eventIndexIndex = 10;

export type ParsedIdCookie = [
string, // cookieDisabled
string, // domainUserId
number, // cookieCreateTs
number, // visitCount
number, // nowTs
number | undefined, // lastVisitTs
string, // sessionId
string, // previousSessionId
string, // firstEventId
number | undefined, // firstEventTs
number // eventIndex
];

export function emptyIdCookie() {
const idCookie: ParsedIdCookie = ['1', '', 0, 0, 0, undefined, '', '', '', undefined, 0];
return idCookie;
Expand Down
2 changes: 1 addition & 1 deletion libraries/browser-tracker-core/src/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
ClearUserDataConfiguration,
ClientSession,
ExtendedCrossDomainLinkerOptions,
ParsedIdCookie,
} from './types';
import {
parseIdCookie,
Expand All @@ -59,7 +60,6 @@ import {
updateFirstEventInIdCookie,
visitCountFromIdCookie,
cookiesEnabledInIdCookie,
ParsedIdCookie,
clientSessionFromIdCookie,
incrementEventIndexInIdCookie,
emptyIdCookie,
Expand Down
27 changes: 22 additions & 5 deletions libraries/browser-tracker-core/src/tracker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,23 @@ export interface BrowserPluginConfiguration extends CorePluginConfiguration {
plugin: BrowserPlugin;
}

/**
* The format of state elements stored in the `id` cookie.
*/
export type ParsedIdCookie = [
cookieDisabled: string,
domainUserId: string,
cookieCreateTs: number,
visitCount: number,
nowTs: number,
lastVisitTs: number | undefined,
sessionId: string,
previousSessionId: string,
firstEventId: string,
firstEventTs: number | undefined,
eventIndex: number
];

/**
* The Browser Tracker
*/
Expand All @@ -383,7 +400,7 @@ export interface BrowserTracker {
*
* @returns Domain session index
*/
getDomainSessionIndex: () => void;
getDomainSessionIndex: () => number;

/**
* Get the current page view ID
Expand All @@ -404,28 +421,28 @@ export interface BrowserTracker {
*
* @returns Cookie name
*/
getCookieName: (basename: string) => void;
getCookieName: (basename: string) => string;

/**
* Get the current user ID (as set previously with setUserId()).
*
* @returns Business-defined user ID
*/
getUserId: () => void;
getUserId: () => string | null | undefined;

/**
* Get visitor ID (from first party cookie)
*
* @returns Visitor ID (or null, if not yet known)
*/
getDomainUserId: () => void;
getDomainUserId: () => string;

/**
* Get the visitor information (from first party cookie)
*
* @returns The domain user information array
*/
getDomainUserInfo: () => void;
getDomainUserInfo: () => ParsedIdCookie;

/**
* Override referrer
Expand Down
2 changes: 2 additions & 0 deletions trackers/browser-tracker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
EventBatch,
GetBatch,
PostBatch,
ParsedIdCookie,
} from '@snowplow/browser-tracker-core';
import { version } from '@snowplow/tracker-core';

Expand Down Expand Up @@ -89,6 +90,7 @@ export {
EventBatch,
GetBatch,
PostBatch,
ParsedIdCookie,
};
export { version };
export * from './api';

0 comments on commit 71a1108

Please sign in to comment.