saasco-sdk 0.1.44 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './lib/analytics';
2
- export * from './lib/getBrowserContext';
3
- export * from './lib/tracking';
4
- export * from './lib/timezones';
5
- export * from './lib/integrations';
@@ -1,134 +0,0 @@
1
- import { FacebookPixelIntegrationConfig } from './integrations/facebook-pixel';
2
- import { PinterestTagIntegrationConfig } from './integrations/pinterest-tag';
3
- import { TikTokPixelIntegrationConfig } from './integrations/tiktok-pixel';
4
- declare global {
5
- interface Window {
6
- saascoAutoPageTrackingActive?: boolean;
7
- saasco: Saasco;
8
- }
9
- }
10
- type Identity = {
11
- userId: string | number;
12
- anonymousId?: string | number;
13
- } | {
14
- anonymousId: string | number;
15
- userId?: string | number;
16
- };
17
- type Context = {
18
- active?: boolean;
19
- };
20
- type TrackPayload = Identity & {
21
- event: string;
22
- sessionId?: string;
23
- properties?: Record<string, any>;
24
- context?: Context;
25
- };
26
- type DoRequestResponse = {
27
- success: boolean;
28
- message: string;
29
- };
30
- export type IntegrationConfigBase = {
31
- type: string;
32
- config: Record<string, any>;
33
- debug?: boolean;
34
- };
35
- export type IntegrationsConfig = (FacebookPixelIntegrationConfig | PinterestTagIntegrationConfig | TikTokPixelIntegrationConfig)[];
36
- export declare class Saasco {
37
- private config;
38
- private lastPageViewPath;
39
- private isInitialized;
40
- private integrationManager;
41
- private logger;
42
- /**
43
- * Creates an instance of Saasco SDK.
44
- * @param config Configuration options.
45
- * @param config.projectId The unique identifier for the project.
46
- * @param config.proxy The URL of the proxy server to use, if any.
47
- * @param config.autoPageTracking Whether to automatically track page views. Default is false.
48
- * @param config.enabled Whether analytics is enabled. Default is true. Set to false for development and staging envioronments. Will still allow debug mode to be true, just no events will be sent
49
- * @param config.debug Whether to log debug information. Default is false.
50
- * @param config.trackUrlParams Whether to track URL parameters. Default is true.
51
- * @param config.trackHashChanges Whether to track hash changes. Default is true.
52
- * @param config.integrations Configuration for third-party integrations like Facebook Pixel.
53
- */
54
- constructor(config: {
55
- projectId: string;
56
- proxy?: string;
57
- autoPageTracking?: {
58
- enabled: boolean;
59
- trackQueryParams?: boolean;
60
- trackHash?: boolean;
61
- };
62
- enabled?: boolean;
63
- debug?: boolean;
64
- debugVerbose?: boolean;
65
- integrations?: IntegrationsConfig;
66
- });
67
- init(): void;
68
- disableDebug(): void;
69
- enableDebug(): void;
70
- /**
71
- * Initialize third-party integrations
72
- */
73
- private initIntegrations;
74
- /**
75
- * Track events with support for both client (Segment-style) and server (object-style) usage
76
- * Client: track('User Signed Up', { plan: 'Pro' }, { source: 'client' })
77
- * Server: track({ event: 'User Signed Up', userId: 'user_123', properties: { plan: 'Pro' } })
78
- */
79
- track(action: string, properties?: Record<string, any>, context?: Context): Promise<DoRequestResponse>;
80
- track(payload: TrackPayload): Promise<DoRequestResponse>;
81
- /**
82
- * The page method lets you record page views on your website
83
- * This records the page title and path and names the event useing the reserved property "Page Viewed"
84
- *
85
- * Before implementing this make sure you have disabled the autoPageTracking in the config or you will get duplicate page views
86
- */
87
- page(): Promise<DoRequestResponse> | undefined;
88
- /**
89
- * The identify method lets you tie a user to their actions and record traits about them.
90
- * We recommend you call this when the user logs in and when any traits get updated.
91
- * You can also identify a user as null when they logout to clear the user
92
- *
93
- * @param distinctId The user's id. This should be the user id from your database. This is optional and can be skipped, but you must provide an email address on the user properties.
94
- * @param properties A dictionary of traits you know about the user like their email, name, plan etc.
95
- * @param context Context for the identify call such as whether the user is active.
96
- */
97
- identify(properties: Record<string, any>, context?: Context): Promise<DoRequestResponse>;
98
- identify(distinctId: string | number | null, properties?: Record<string, any>, context?: Context): Promise<DoRequestResponse>;
99
- /**
100
- * This should only be called when the user logs out
101
- * It will reset the session, anonymous id, and user id
102
- *
103
- * All events after calling reset will be tracked as a new user
104
- *
105
- */
106
- logout(): void;
107
- /**
108
- * Handles sending data to the Saasco API
109
- * If you have a proxy set up, it will send the data to the proxy and you can handle forawrding the data to the Saasco events API
110
- * @param path
111
- * @param data
112
- */
113
- private doRequest;
114
- /**
115
- * If autoPageTracking is enabled, this will automatically track page views
116
- * It listens to url changes to track new pages every time the url changes
117
- * @returns
118
- */
119
- private initAutoPageTracking;
120
- getIntegrationsStats(): {
121
- currentEnvironment: import("./integrations/integration-manager").IntegrationEnvironment;
122
- integrations: {
123
- name: string;
124
- status: import("./integrations/integration-manager").IntegrationStatus;
125
- environments: import("./integrations/integration-manager").IntegrationEnvironment[];
126
- }[];
127
- queueLength: number;
128
- readyCount: number;
129
- periodicFlushEnabled: boolean;
130
- flushInterval: number;
131
- context: import("./integrations/integration-manager").AnalyticsContext;
132
- };
133
- }
134
- export {};
@@ -1,2 +0,0 @@
1
- import { BrowserContext } from './tracking';
2
- export declare function getBrowserContext(): BrowserContext;
@@ -1,34 +0,0 @@
1
- import { IntegrationConfigBase } from '../analytics';
2
- import { Integration } from './integration-manager';
3
- declare global {
4
- interface Window {
5
- fbq?: Fbq;
6
- _fbq?: Fbq;
7
- }
8
- interface Fbq {
9
- (...args: any[]): void;
10
- queue?: any[];
11
- loaded?: boolean;
12
- version?: string;
13
- disablePushState?: boolean;
14
- allowDuplicatePageViews?: boolean;
15
- }
16
- }
17
- export type FacebookEventMapping = {
18
- [key: string]: StandardFacebookEvent;
19
- };
20
- export type FacebookPixelIntegrationConfig = IntegrationConfigBase & {
21
- type: 'facebook-pixel';
22
- config: FacebookPixelConfig;
23
- };
24
- export type FacebookPixelConfig = {
25
- pixelId: string;
26
- eventMapping?: FacebookEventMapping;
27
- automaticConfiguration?: boolean;
28
- };
29
- export declare const standardFacebookEvents: readonly ["AddPaymentInfo", "AddToCart", "AddToWishlist", "CompleteRegistration", "Contact", "CustomizeProduct", "Donate", "FindLocation", "InitiateCheckout", "Lead", "Purchase", "Schedule", "Search", "StartTrial", "SubmitApplication", "Subscribe", "ViewContent", "PageView"];
30
- export type StandardFacebookEvent = (typeof standardFacebookEvents)[number];
31
- /**
32
- * Create a Facebook Pixel integration instance
33
- */
34
- export declare function createFacebookPixelIntegration(config: FacebookPixelConfig, debug?: boolean): Integration;
@@ -1,4 +0,0 @@
1
- export * from './integration-manager';
2
- export * from './facebook-pixel';
3
- export * from './pinterest-tag';
4
- export * from './tiktok-pixel';
@@ -1,119 +0,0 @@
1
- import { LogLevel } from '../utils';
2
- export type EventType = 'identify' | 'track';
3
- export type AnalyticsContext = {
4
- distinctId?: string | null;
5
- anonymousId?: string | null;
6
- sessionId?: string | null;
7
- [key: string]: unknown;
8
- };
9
- export type EventEnvelope = {
10
- id: string;
11
- type: EventType;
12
- timestamp: number;
13
- name?: string;
14
- properties?: Record<string, unknown>;
15
- context: AnalyticsContext;
16
- };
17
- export type IntegrationEnvironment = 'client' | 'server';
18
- export type Integration = {
19
- /**
20
- * The name of the integration
21
- */
22
- name: string;
23
- /**
24
- * The environments where this integration can run
25
- */
26
- environments: IntegrationEnvironment[];
27
- /**
28
- * The function to initialize the integration
29
- */
30
- init?: (context?: AnalyticsContext) => Promise<void> | void;
31
- /**
32
- * The function to track an event
33
- */
34
- track?: (name: string, properties?: Record<string, unknown>, context?: AnalyticsContext) => void;
35
- /**
36
- * The function to identify a user
37
- */
38
- identify?: (userId?: string | null, properties?: Record<string, unknown>, context?: AnalyticsContext) => void | Promise<void>;
39
- };
40
- export type IntegrationStatus = 'idle' | 'loading' | 'ready' | 'error';
41
- export type IntegrationState = {
42
- integration: Integration;
43
- status: IntegrationStatus;
44
- };
45
- export type ManagerConfig = {
46
- /**
47
- * Enable logger level
48
- */
49
- loggerLevel?: LogLevel;
50
- /**
51
- * Max queue size, once it exceeds this number, the oldest event will be dropped
52
- */
53
- maxQueueSize?: number;
54
- /**
55
- * Max integration wait time, this is how long we will wait for an integration to be ready before flushing the queue. If an integration is not ready after this time it will miss any previous events.
56
- */
57
- maxIntegrationWaitTime?: number;
58
- /**
59
- * Periodic flush interval in milliseconds. Set to 0 to disable periodic flushing.
60
- */
61
- flushInterval?: number;
62
- };
63
- export declare class IntegrationManager {
64
- private context;
65
- private integrations;
66
- private globalQueue;
67
- private config;
68
- private logger;
69
- private initTime;
70
- private currentEnvironment;
71
- private flushTimer?;
72
- private unloadHandler?;
73
- constructor(config?: ManagerConfig);
74
- /**
75
- * Shallow-merge context to keep it simple + predictable in v0
76
- */
77
- setContext(next: Partial<AnalyticsContext>): void;
78
- /**
79
- * Register and init an integration. When init resolves, we mark it ready and
80
- * immediately flush any queued events in FIFO order to *all* ready integrations.
81
- */
82
- registerIntegration(integration: Integration): Promise<void>;
83
- identify(userId?: string | null, traits?: Record<string, unknown>): void;
84
- track(name: string, properties?: Record<string, unknown>): void;
85
- /**
86
- * Core send path: if at least one integration is ready -> deliver immediately
87
- * Else enqueue (bounded FIFO)
88
- */
89
- private send;
90
- private deliver;
91
- /**
92
- * Flush queued events FIFO once at least one integration is ready.
93
- */
94
- private flush;
95
- private readyCount;
96
- private isReady;
97
- /**
98
- * Setup periodic flushing if enabled
99
- */
100
- private setupPeriodicFlushing;
101
- /**
102
- * Setup page unload handler for client environment
103
- */
104
- private setupUnloadHandler;
105
- /** Debug helpers */
106
- getStats(): {
107
- currentEnvironment: IntegrationEnvironment;
108
- integrations: {
109
- name: string;
110
- status: IntegrationStatus;
111
- environments: IntegrationEnvironment[];
112
- }[];
113
- queueLength: number;
114
- readyCount: number;
115
- periodicFlushEnabled: boolean;
116
- flushInterval: number;
117
- context: AnalyticsContext;
118
- };
119
- }
@@ -1,37 +0,0 @@
1
- import { IntegrationConfigBase } from '../analytics';
2
- import { Integration } from './integration-manager';
3
- declare global {
4
- interface Window {
5
- pintrk?: Pintrk;
6
- _pintrk?: Pintrk;
7
- }
8
- interface Pintrk {
9
- (...args: any[]): void;
10
- queue?: any[];
11
- loaded?: boolean;
12
- version?: string;
13
- }
14
- }
15
- export type PinterestEventMapping = {
16
- [key: string]: StandardPinterestEvent;
17
- };
18
- export type PinterestTagIntegrationConfig = IntegrationConfigBase & {
19
- type: 'pinterest-tag';
20
- config: PinterestTagConfig;
21
- };
22
- export type PinterestTagConfig = {
23
- tagId: string;
24
- eventMapping?: PinterestEventMapping;
25
- automaticConfiguration?: boolean;
26
- };
27
- /**
28
- * Standard Pinterest events
29
- * Reference: https://www.pinterest.com/_/_/help/business/article/event-code
30
- */
31
- export declare const standardPinterestEvents: readonly ["checkout", "addtocart", "pagevisit", "signup", "watchvideo", "lead", "search", "viewcategory", "custom", "addpaymentinfo", "addtowishlist", "initiatecheckout", "subscribe", "viewcontent"];
32
- export type StandardPinterestEvent = (typeof standardPinterestEvents)[number];
33
- /**
34
- * Create a Pinterest Tag integration instance
35
- * Documentation: https://help.pinterest.com/en/business/article/install-the-pinterest-tag
36
- */
37
- export declare function createPinterestTagIntegration(config: PinterestTagConfig, debug?: boolean): Integration;
@@ -1,59 +0,0 @@
1
- /**
2
- * TikTok Pixel Integration
3
- *
4
- * This integration provides TikTok Pixel tracking capabilities including:
5
- * - Event tracking for standard and custom events
6
- * - Advanced matching with hashed user identification data
7
- * - Automatic script loading and initialization
8
- *
9
- * Key Resources:
10
- * - Advanced Matching: https://business-api.tiktok.com/portal/docs?rid=5ipocbxyw8v&id=1739585700402178
11
- * - Standard Events: https://business-api.tiktok.com/portal/docs?id=1771101186666498
12
- */
13
- import { IntegrationConfigBase } from '../analytics';
14
- import { Integration } from './integration-manager';
15
- declare global {
16
- interface Window {
17
- ttq?: Ttq;
18
- TiktokAnalyticsObject?: string;
19
- }
20
- interface Ttq {
21
- (...args: any[]): void;
22
- methods?: string[];
23
- _i?: Record<string, any>;
24
- _t?: Record<string, number>;
25
- _o?: Record<string, any>;
26
- load?: (pixelId: string, options?: any) => void;
27
- page?: () => void;
28
- track?: (eventName: string, properties?: Record<string, unknown>) => void;
29
- identify?: (properties?: Record<string, unknown>) => void;
30
- setAndDefer?: (obj: any, method: string) => void;
31
- instance?: (pixelId: string) => any;
32
- }
33
- }
34
- export type TikTokEventMapping = {
35
- [key: string]: StandardTikTokEvent;
36
- };
37
- export type TikTokPixelIntegrationConfig = IntegrationConfigBase & {
38
- type: 'tiktok-pixel';
39
- config: TikTokPixelConfig;
40
- };
41
- export type TikTokPixelConfig = {
42
- pixelId: string;
43
- eventMapping?: TikTokEventMapping;
44
- testMode?: boolean;
45
- };
46
- /**
47
- * Standard TikTok events supported by the pixel
48
- *
49
- * Note: This list reflects TikTok's official standard events. Events like 'CompletePayment'
50
- * are not standard events and should be tracked as custom events if needed.
51
- *
52
- * Documentation: https://business-api.tiktok.com/portal/docs?id=1771101186666498
53
- */
54
- export declare const standardTikTokEvents: readonly ["AddPaymentInfo", "AddToCart", "AddToWishlist", "ApplicationApproval", "CompleteRegistration", "Contact", "CustomizeProduct", "Download", "FindLocation", "InitiateCheckout", "Lead", "Purchase", "Schedule", "Search", "StartTrial", "SubmitApplication", "Subscribe", "ViewContent", "PageView"];
55
- export type StandardTikTokEvent = (typeof standardTikTokEvents)[number];
56
- /**
57
- * Create a TikTok Pixel integration instance
58
- */
59
- export declare function createTikTokPixelIntegration(config: TikTokPixelConfig, debug?: boolean): Integration;
@@ -1,6 +0,0 @@
1
- import { Saasco } from './analytics';
2
- declare global {
3
- interface Window {
4
- saasco: Saasco;
5
- }
6
- }
@@ -1,3 +0,0 @@
1
- export declare const timezones: {
2
- [key: string]: string;
3
- };
@@ -1 +0,0 @@
1
- export * from './types';
@@ -1,73 +0,0 @@
1
- import { z } from 'zod';
2
- export declare const browserContextSchema: z.ZodObject<{
3
- $locale: z.ZodString;
4
- $location: z.ZodString;
5
- $href: z.ZodString;
6
- $pathname: z.ZodString;
7
- $referrer: z.ZodString;
8
- $screenDPI: z.ZodNumber;
9
- $screenHeight: z.ZodNumber;
10
- $screenWidth: z.ZodNumber;
11
- $title: z.ZodString;
12
- $userAgent: z.ZodString;
13
- $utmSource: z.ZodNullable<z.ZodString>;
14
- $utmMedium: z.ZodNullable<z.ZodString>;
15
- $utmCampaign: z.ZodNullable<z.ZodString>;
16
- $utmTerm: z.ZodNullable<z.ZodString>;
17
- $utmContent: z.ZodNullable<z.ZodString>;
18
- $utmId: z.ZodNullable<z.ZodString>;
19
- $utmSourcePlatform: z.ZodNullable<z.ZodString>;
20
- $utmCampaignId: z.ZodNullable<z.ZodString>;
21
- $utmCreativeFormat: z.ZodNullable<z.ZodString>;
22
- $utmMarketingTactic: z.ZodNullable<z.ZodString>;
23
- $utmAdSource: z.ZodNullable<z.ZodString>;
24
- $utmAdId: z.ZodNullable<z.ZodString>;
25
- }, "strip", z.ZodTypeAny, {
26
- $locale: string;
27
- $location: string;
28
- $href: string;
29
- $pathname: string;
30
- $referrer: string;
31
- $screenDPI: number;
32
- $screenHeight: number;
33
- $screenWidth: number;
34
- $title: string;
35
- $userAgent: string;
36
- $utmSource: string | null;
37
- $utmMedium: string | null;
38
- $utmCampaign: string | null;
39
- $utmTerm: string | null;
40
- $utmContent: string | null;
41
- $utmId: string | null;
42
- $utmSourcePlatform: string | null;
43
- $utmCampaignId: string | null;
44
- $utmCreativeFormat: string | null;
45
- $utmMarketingTactic: string | null;
46
- $utmAdSource: string | null;
47
- $utmAdId: string | null;
48
- }, {
49
- $locale: string;
50
- $location: string;
51
- $href: string;
52
- $pathname: string;
53
- $referrer: string;
54
- $screenDPI: number;
55
- $screenHeight: number;
56
- $screenWidth: number;
57
- $title: string;
58
- $userAgent: string;
59
- $utmSource: string | null;
60
- $utmMedium: string | null;
61
- $utmCampaign: string | null;
62
- $utmTerm: string | null;
63
- $utmContent: string | null;
64
- $utmId: string | null;
65
- $utmSourcePlatform: string | null;
66
- $utmCampaignId: string | null;
67
- $utmCreativeFormat: string | null;
68
- $utmMarketingTactic: string | null;
69
- $utmAdSource: string | null;
70
- $utmAdId: string | null;
71
- }>;
72
- export type BrowserContext = z.infer<typeof browserContextSchema>;
73
- export type SuperContext = Record<string, any>;
@@ -1,8 +0,0 @@
1
- import { LogLevel } from './logger';
2
- export declare const isServer: boolean;
3
- /**
4
- * Get the logger level for a specific integration
5
- * Supports URL parameters like saasco-debug-{integration-type}=true
6
- * If debug is true or URL param is true, sets level to DEBUG (3), otherwise WARN
7
- */
8
- export declare function getIntegrationLoggerLevel(integrationName: string, debug?: boolean): LogLevel;
@@ -1,3 +0,0 @@
1
- export * from './logger';
2
- export * from './getIntegrationLoggerLevel';
3
- export * from './uuid';
@@ -1,30 +0,0 @@
1
- export declare enum LogLevel {
2
- ERROR = 0,
3
- WARN = 1,
4
- INFO = 2,
5
- DEBUG = 3
6
- }
7
- export type AnalyticsLoggerConfig = {
8
- level: LogLevel;
9
- label: string;
10
- };
11
- export declare class AnalyticsLogger {
12
- private config;
13
- constructor(config: AnalyticsLoggerConfig);
14
- debug(...args: any[]): void;
15
- /**
16
- * Log debug information
17
- */
18
- info(...args: any[]): void;
19
- /**
20
- * Log warning information
21
- */
22
- warn(...args: any[]): void;
23
- /**
24
- * Log error information
25
- */
26
- error(...args: any[]): {
27
- success: boolean;
28
- message: string;
29
- };
30
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * Generate a collision-resistant UUID
3
- * Uses lukeed's UUID v4 implementation for consistent, fast UUID generation
4
- */
5
- export declare function uuid(): string;