saasco-sdk 0.1.9 → 0.1.11

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/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  - [Privacy](#privacy)
6
6
  - [Getting Started](#initiate-the-lib)
7
- - [Automatic Page View Tracking](#automatic-page-view-tracking)
7
+ - [Init Automatic Page View Tracking](#init-automatic-page-view-tracking)
8
8
  - [Tracking Events](#tracking-events)
9
9
  - [Identifying Users](#identifying-users)
10
10
  - [Debugging and Dev](#debugging-and-dev)
@@ -27,22 +27,25 @@ Copy your Project ID from the project settings page in Saasco:
27
27
  Then import Saasco and initiate the lib with your Project ID.
28
28
 
29
29
  ```ts
30
-
31
30
  // lib/saasco.ts
32
31
  import { Saasco } from 'saasco-sdk';
33
32
 
34
- // Initiate the lib
35
- const saasco = new Saasco({ projectId: "YOUR-PROJECT-ID" });
36
- saasco.init();
37
-
38
- export saasco;
33
+ export const saasco = new Saasco({ projectId: 'YOUR-PROJECT-ID' });
39
34
  ```
40
35
 
41
- ## Automatic Page View Tracking
36
+ ## Init Automatic Page View Tracking
42
37
 
43
- When you initial the lib live above Saasco will automatically start tracking all page views in your app. There is no other configuration. It will automatically track url changes, even for SPAs like NextJs and Vue.
38
+ When you initial the lib Saasco will automatically start tracking all page views in your app. There is no other configuration. It will automatically track url changes, even for SPAs like NextJs and Vue.
44
39
  You can also [manually track pages](https://www.notion.so/Manual-Page-Tracking-in-SPAs-2442fc7586dc4208ae8f669eb7561b1a?pvs=21) by opting our of automatic page tracking. For most use cases you don't need to do this.
45
40
 
41
+ ```ts
42
+ // ./app/index
43
+ import saasco from './lib/saasco';
44
+
45
+ // This should be called once in your app and will start auto page tracking if you have it enabled.
46
+ saasco.init();
47
+ ```
48
+
46
49
  ## Tracking Events
47
50
 
48
51
  With events you can track custom actions users are taking on your site with event properties.
package/index.cjs.js CHANGED
@@ -434,6 +434,8 @@ const timezones = {
434
434
  'Asia/Calcutta': 'IN'
435
435
  };
436
436
 
437
+ var version = "0.1.11";
438
+
437
439
  const isBrowser = typeof window !== 'undefined';
438
440
  let userId = null;
439
441
  const PREF = 'saasco-sdk';
@@ -553,13 +555,21 @@ class Saasco {
553
555
  this.log('Saasco is already initialized. Please check your code to ensure that init() is not being called multiple times.');
554
556
  return;
555
557
  }
558
+ // Export Saasco to the window object for easy access and debugging
559
+ if (isBrowser) {
560
+ window.saasco = this;
561
+ }
556
562
  this.log('Saasco initialized', this.config);
557
563
  this.initiAutoPageTracking();
558
564
  this.isInitialized = true;
559
565
  }
560
- debug(value) {
561
- this.config.debug = value;
562
- this.log(`Debug mode ${value ? 'activated' : 'deactivated'}.`);
566
+ disableDebug() {
567
+ this.log('Debug mode deactivated.');
568
+ this.config.debug = false;
569
+ }
570
+ enableDebug() {
571
+ this.config.debug = true;
572
+ this.log('Debug mode activated.');
563
573
  }
564
574
  /**
565
575
  * The track method lets you record the actions your users perform.
@@ -590,7 +600,7 @@ class Saasco {
590
600
  id: uuid.v4(),
591
601
  timestamp: new Date().toISOString(),
592
602
  action: name,
593
- version: '2',
603
+ version,
594
604
  sessionId: getSessionId(),
595
605
  anonymousId: getAnonymousId(),
596
606
  distinctId: userId,
@@ -619,12 +629,9 @@ class Saasco {
619
629
  // When the user gets identified as null this will reset the users session and anonymous id
620
630
  // This should only be called when distinctId is null and there is an existing userId.
621
631
  // This means the user has logged out and we should reset the session and anonymous IDs
622
- const userIdChangedToNull = !distinctId && !!userId;
632
+ const userIdChangedToNull = distinctId === null && !!userId;
623
633
  if (userIdChangedToNull) this.log('User ID change to null. Session reset');
624
- // If we have a userId, but a new user ID is provided, we should reset the session and anonymous IDs
625
- const userIdChanged = !!userId && distinctId !== userId;
626
- if (userIdChanged) this.log('User ID changed. Session reset');
627
- const reset = userIdChangedToNull || userIdChanged;
634
+ const reset = userIdChangedToNull;
628
635
  setSessionId({
629
636
  reset
630
637
  });
@@ -641,7 +648,7 @@ class Saasco {
641
648
  projectId: this.config.projectId,
642
649
  distinctId,
643
650
  anonymousId,
644
- version: '2',
651
+ version,
645
652
  payload: properties || {},
646
653
  context: context || {}
647
654
  };
@@ -791,8 +798,15 @@ function coerceReservedProperties(properties) {
791
798
  }) => {
792
799
  const coercedValue = coerceValue(properties, coerceFrom, schema);
793
800
  if (coercedValue) acc[key] = coercedValue;
794
- if (!coercedValue && key === 'displayName' && acc['firstName']) {
795
- const displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
801
+ if (!coercedValue && key === 'displayName') {
802
+ let displayName = '';
803
+ if (acc['firstName']) {
804
+ displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
805
+ } else if (acc['username'] && typeof acc['username'] === 'string') {
806
+ displayName = acc['username'];
807
+ } else if (acc['name'] && typeof acc['name'] === 'string') {
808
+ displayName = acc['name'];
809
+ }
796
810
  if (displayName) acc[key] = displayName;
797
811
  }
798
812
  return acc;
@@ -885,16 +899,17 @@ const kpisRequestSchema = zod.z.object({
885
899
  referrer: zod.z.string().optional(),
886
900
  location: zod.z.string().optional()
887
901
  });
902
+ const kpiSchema = zod.z.object({
903
+ date: zod.z.string(),
904
+ users: zod.z.number(),
905
+ sessions: zod.z.number(),
906
+ pageViews: zod.z.number(),
907
+ events: zod.z.number(),
908
+ bounceRate: zod.z.nullable(zod.z.number()),
909
+ avgSessionSec: zod.z.number()
910
+ });
888
911
  const kpisResponseSchema = tinyBirdBaseSchema.extend({
889
- data: zod.z.array(zod.z.object({
890
- date: zod.z.string(),
891
- users: zod.z.number(),
892
- sessions: zod.z.number(),
893
- pageViews: zod.z.number(),
894
- events: zod.z.number(),
895
- bounceRate: zod.z.nullable(zod.z.number()),
896
- avgSessionSec: zod.z.number()
897
- }))
912
+ data: zod.z.array(kpiSchema)
898
913
  });
899
914
  const baseTopRequestSchema = zod.z.object({
900
915
  projectId: zod.z.string(),
@@ -964,7 +979,7 @@ const analyticsEventSchema = zod.z.object({
964
979
  projectId: zod.z.string(),
965
980
  anonymousId: zod.z.string(),
966
981
  sessionId: zod.z.string(),
967
- distinctId: zod.z.string().optional(),
982
+ distinctId: zod.z.string().nullable(),
968
983
  version: zod.z.string(),
969
984
  action: zod.z.string(),
970
985
  payload: zod.z.string()
@@ -974,7 +989,7 @@ const userEventsResponseSchema = tinyBirdBaseSchema.extend({
974
989
  });
975
990
  const currentUsersRequestSchema = zod.z.object({
976
991
  projectId: zod.z.string(),
977
- minutes: zod.z.number().optional(),
992
+ minutes: zod.z.number(),
978
993
  referrer: zod.z.string().optional(),
979
994
  location: zod.z.string().optional()
980
995
  });
@@ -1057,6 +1072,7 @@ exports.currentUsersRequestSchema = currentUsersRequestSchema;
1057
1072
  exports.currentUsersResponseSchema = currentUsersResponseSchema;
1058
1073
  exports.getAnalyticsUserParamsSchema = getAnalyticsUserParamsSchema;
1059
1074
  exports.idenfitySchema = idenfitySchema;
1075
+ exports.kpiSchema = kpiSchema;
1060
1076
  exports.kpisRequestSchema = kpisRequestSchema;
1061
1077
  exports.kpisResponseSchema = kpisResponseSchema;
1062
1078
  exports.latestEventItemSchema = latestEventItemSchema;
package/index.esm.js CHANGED
@@ -430,6 +430,8 @@ const timezones = {
430
430
  'Asia/Calcutta': 'IN'
431
431
  };
432
432
 
433
+ var version = "0.1.11";
434
+
433
435
  const isBrowser = typeof window !== 'undefined';
434
436
  let userId = null;
435
437
  const PREF = 'saasco-sdk';
@@ -549,13 +551,21 @@ class Saasco {
549
551
  this.log('Saasco is already initialized. Please check your code to ensure that init() is not being called multiple times.');
550
552
  return;
551
553
  }
554
+ // Export Saasco to the window object for easy access and debugging
555
+ if (isBrowser) {
556
+ window.saasco = this;
557
+ }
552
558
  this.log('Saasco initialized', this.config);
553
559
  this.initiAutoPageTracking();
554
560
  this.isInitialized = true;
555
561
  }
556
- debug(value) {
557
- this.config.debug = value;
558
- this.log(`Debug mode ${value ? 'activated' : 'deactivated'}.`);
562
+ disableDebug() {
563
+ this.log('Debug mode deactivated.');
564
+ this.config.debug = false;
565
+ }
566
+ enableDebug() {
567
+ this.config.debug = true;
568
+ this.log('Debug mode activated.');
559
569
  }
560
570
  /**
561
571
  * The track method lets you record the actions your users perform.
@@ -586,7 +596,7 @@ class Saasco {
586
596
  id: v4(),
587
597
  timestamp: new Date().toISOString(),
588
598
  action: name,
589
- version: '2',
599
+ version,
590
600
  sessionId: getSessionId(),
591
601
  anonymousId: getAnonymousId(),
592
602
  distinctId: userId,
@@ -615,12 +625,9 @@ class Saasco {
615
625
  // When the user gets identified as null this will reset the users session and anonymous id
616
626
  // This should only be called when distinctId is null and there is an existing userId.
617
627
  // This means the user has logged out and we should reset the session and anonymous IDs
618
- const userIdChangedToNull = !distinctId && !!userId;
628
+ const userIdChangedToNull = distinctId === null && !!userId;
619
629
  if (userIdChangedToNull) this.log('User ID change to null. Session reset');
620
- // If we have a userId, but a new user ID is provided, we should reset the session and anonymous IDs
621
- const userIdChanged = !!userId && distinctId !== userId;
622
- if (userIdChanged) this.log('User ID changed. Session reset');
623
- const reset = userIdChangedToNull || userIdChanged;
630
+ const reset = userIdChangedToNull;
624
631
  setSessionId({
625
632
  reset
626
633
  });
@@ -637,7 +644,7 @@ class Saasco {
637
644
  projectId: this.config.projectId,
638
645
  distinctId,
639
646
  anonymousId,
640
- version: '2',
647
+ version,
641
648
  payload: properties || {},
642
649
  context: context || {}
643
650
  };
@@ -787,8 +794,15 @@ function coerceReservedProperties(properties) {
787
794
  }) => {
788
795
  const coercedValue = coerceValue(properties, coerceFrom, schema);
789
796
  if (coercedValue) acc[key] = coercedValue;
790
- if (!coercedValue && key === 'displayName' && acc['firstName']) {
791
- const displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
797
+ if (!coercedValue && key === 'displayName') {
798
+ let displayName = '';
799
+ if (acc['firstName']) {
800
+ displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
801
+ } else if (acc['username'] && typeof acc['username'] === 'string') {
802
+ displayName = acc['username'];
803
+ } else if (acc['name'] && typeof acc['name'] === 'string') {
804
+ displayName = acc['name'];
805
+ }
792
806
  if (displayName) acc[key] = displayName;
793
807
  }
794
808
  return acc;
@@ -881,16 +895,17 @@ const kpisRequestSchema = z.object({
881
895
  referrer: z.string().optional(),
882
896
  location: z.string().optional()
883
897
  });
898
+ const kpiSchema = z.object({
899
+ date: z.string(),
900
+ users: z.number(),
901
+ sessions: z.number(),
902
+ pageViews: z.number(),
903
+ events: z.number(),
904
+ bounceRate: z.nullable(z.number()),
905
+ avgSessionSec: z.number()
906
+ });
884
907
  const kpisResponseSchema = tinyBirdBaseSchema.extend({
885
- data: z.array(z.object({
886
- date: z.string(),
887
- users: z.number(),
888
- sessions: z.number(),
889
- pageViews: z.number(),
890
- events: z.number(),
891
- bounceRate: z.nullable(z.number()),
892
- avgSessionSec: z.number()
893
- }))
908
+ data: z.array(kpiSchema)
894
909
  });
895
910
  const baseTopRequestSchema = z.object({
896
911
  projectId: z.string(),
@@ -960,7 +975,7 @@ const analyticsEventSchema = z.object({
960
975
  projectId: z.string(),
961
976
  anonymousId: z.string(),
962
977
  sessionId: z.string(),
963
- distinctId: z.string().optional(),
978
+ distinctId: z.string().nullable(),
964
979
  version: z.string(),
965
980
  action: z.string(),
966
981
  payload: z.string()
@@ -970,7 +985,7 @@ const userEventsResponseSchema = tinyBirdBaseSchema.extend({
970
985
  });
971
986
  const currentUsersRequestSchema = z.object({
972
987
  projectId: z.string(),
973
- minutes: z.number().optional(),
988
+ minutes: z.number(),
974
989
  referrer: z.string().optional(),
975
990
  location: z.string().optional()
976
991
  });
@@ -1041,4 +1056,4 @@ const latestIdentifiesResponseSchema = tinyBirdBaseSchema.extend({
1041
1056
  }))
1042
1057
  });
1043
1058
 
1044
- export { Saasco, TinybirdJobResponseSchema, analyticsEventSchema, analyticsUserRawSchema, analyticsUserSchema, baseTopRequestSchema, coerceReservedProperties, contactPropertiesSchema, currentUsersRequestSchema, currentUsersResponseSchema, getAnalyticsUserParamsSchema, idenfitySchema, kpisRequestSchema, kpisResponseSchema, latestEventItemSchema, latestEventsRequestSchema, latestEventsResponseSchema, latestIdentifiesRequestSchema, latestIdentifiesResponseSchema, listUsersParamsSchema, reservedPropertiesSchema, tbAny, timezones, tinyBirdBaseSchema, topActionsResponseSchema, topBrowsersResponseSchema, topDevicesResponseSchema, topLocationsResponseSchema, topPagesResponseSchema, topSourcesResponseSchema, topSourcesSchema, userEventsResponseSchema };
1059
+ export { Saasco, TinybirdJobResponseSchema, analyticsEventSchema, analyticsUserRawSchema, analyticsUserSchema, baseTopRequestSchema, coerceReservedProperties, contactPropertiesSchema, currentUsersRequestSchema, currentUsersResponseSchema, getAnalyticsUserParamsSchema, idenfitySchema, kpiSchema, kpisRequestSchema, kpisResponseSchema, latestEventItemSchema, latestEventsRequestSchema, latestEventsResponseSchema, latestIdentifiesRequestSchema, latestIdentifiesResponseSchema, listUsersParamsSchema, reservedPropertiesSchema, tbAny, timezones, tinyBirdBaseSchema, topActionsResponseSchema, topBrowsersResponseSchema, topDevicesResponseSchema, topLocationsResponseSchema, topPagesResponseSchema, topSourcesResponseSchema, topSourcesSchema, userEventsResponseSchema };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasco-sdk",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "@lukeed/uuid": "^2.0.1",
@@ -24,7 +24,8 @@ export declare class Saasco {
24
24
  debug?: boolean;
25
25
  });
26
26
  init(): void;
27
- debug(value: boolean): void;
27
+ disableDebug(): void;
28
+ enableDebug(): void;
28
29
  /**
29
30
  * The track method lets you record the actions your users perform.
30
31
  * Its good to keep a conistent naming convention for your events.
@@ -321,6 +321,32 @@ export declare const kpisRequestSchema: z.ZodObject<{
321
321
  dateFrom?: string | undefined;
322
322
  }>;
323
323
  export type KpisRequest = z.infer<typeof kpisRequestSchema>;
324
+ export declare const kpiSchema: z.ZodObject<{
325
+ date: z.ZodString;
326
+ users: z.ZodNumber;
327
+ sessions: z.ZodNumber;
328
+ pageViews: z.ZodNumber;
329
+ events: z.ZodNumber;
330
+ bounceRate: z.ZodNullable<z.ZodNumber>;
331
+ avgSessionSec: z.ZodNumber;
332
+ }, "strip", z.ZodTypeAny, {
333
+ date: string;
334
+ users: number;
335
+ sessions: number;
336
+ pageViews: number;
337
+ events: number;
338
+ bounceRate: number | null;
339
+ avgSessionSec: number;
340
+ }, {
341
+ date: string;
342
+ users: number;
343
+ sessions: number;
344
+ pageViews: number;
345
+ events: number;
346
+ bounceRate: number | null;
347
+ avgSessionSec: number;
348
+ }>;
349
+ export type Kpi = z.infer<typeof kpiSchema>;
324
350
  export declare const kpisResponseSchema: z.ZodObject<z.objectUtil.extendShape<{
325
351
  meta: z.ZodArray<z.ZodObject<{
326
352
  name: z.ZodString;
@@ -957,7 +983,7 @@ export declare const analyticsEventSchema: z.ZodObject<{
957
983
  projectId: z.ZodString;
958
984
  anonymousId: z.ZodString;
959
985
  sessionId: z.ZodString;
960
- distinctId: z.ZodOptional<z.ZodString>;
986
+ distinctId: z.ZodNullable<z.ZodString>;
961
987
  version: z.ZodString;
962
988
  action: z.ZodString;
963
989
  payload: z.ZodString;
@@ -969,8 +995,8 @@ export declare const analyticsEventSchema: z.ZodObject<{
969
995
  action: string;
970
996
  anonymousId: string;
971
997
  sessionId: string;
998
+ distinctId: string | null;
972
999
  version: string;
973
- distinctId?: string | undefined;
974
1000
  }, {
975
1001
  id: string;
976
1002
  projectId: string;
@@ -979,8 +1005,8 @@ export declare const analyticsEventSchema: z.ZodObject<{
979
1005
  action: string;
980
1006
  anonymousId: string;
981
1007
  sessionId: string;
1008
+ distinctId: string | null;
982
1009
  version: string;
983
- distinctId?: string | undefined;
984
1010
  }>;
985
1011
  export type AnalyticsEvent = z.infer<typeof analyticsEventSchema>;
986
1012
  export declare const userEventsResponseSchema: z.ZodObject<z.objectUtil.extendShape<{
@@ -1015,7 +1041,7 @@ export declare const userEventsResponseSchema: z.ZodObject<z.objectUtil.extendSh
1015
1041
  projectId: z.ZodString;
1016
1042
  anonymousId: z.ZodString;
1017
1043
  sessionId: z.ZodString;
1018
- distinctId: z.ZodOptional<z.ZodString>;
1044
+ distinctId: z.ZodNullable<z.ZodString>;
1019
1045
  version: z.ZodString;
1020
1046
  action: z.ZodString;
1021
1047
  payload: z.ZodString;
@@ -1027,8 +1053,8 @@ export declare const userEventsResponseSchema: z.ZodObject<z.objectUtil.extendSh
1027
1053
  action: string;
1028
1054
  anonymousId: string;
1029
1055
  sessionId: string;
1056
+ distinctId: string | null;
1030
1057
  version: string;
1031
- distinctId?: string | undefined;
1032
1058
  }, {
1033
1059
  id: string;
1034
1060
  projectId: string;
@@ -1037,8 +1063,8 @@ export declare const userEventsResponseSchema: z.ZodObject<z.objectUtil.extendSh
1037
1063
  action: string;
1038
1064
  anonymousId: string;
1039
1065
  sessionId: string;
1066
+ distinctId: string | null;
1040
1067
  version: string;
1041
- distinctId?: string | undefined;
1042
1068
  }>, "many">;
1043
1069
  }>, "strip", z.ZodTypeAny, {
1044
1070
  meta: {
@@ -1059,8 +1085,8 @@ export declare const userEventsResponseSchema: z.ZodObject<z.objectUtil.extendSh
1059
1085
  action: string;
1060
1086
  anonymousId: string;
1061
1087
  sessionId: string;
1088
+ distinctId: string | null;
1062
1089
  version: string;
1063
- distinctId?: string | undefined;
1064
1090
  }[];
1065
1091
  }, {
1066
1092
  meta: {
@@ -1081,26 +1107,26 @@ export declare const userEventsResponseSchema: z.ZodObject<z.objectUtil.extendSh
1081
1107
  action: string;
1082
1108
  anonymousId: string;
1083
1109
  sessionId: string;
1110
+ distinctId: string | null;
1084
1111
  version: string;
1085
- distinctId?: string | undefined;
1086
1112
  }[];
1087
1113
  }>;
1088
1114
  export type UserEventsResponse = z.infer<typeof userEventsResponseSchema>;
1089
1115
  export declare const currentUsersRequestSchema: z.ZodObject<{
1090
1116
  projectId: z.ZodString;
1091
- minutes: z.ZodOptional<z.ZodNumber>;
1117
+ minutes: z.ZodNumber;
1092
1118
  referrer: z.ZodOptional<z.ZodString>;
1093
1119
  location: z.ZodOptional<z.ZodString>;
1094
1120
  }, "strip", z.ZodTypeAny, {
1095
1121
  projectId: string;
1122
+ minutes: number;
1096
1123
  location?: string | undefined;
1097
1124
  referrer?: string | undefined;
1098
- minutes?: number | undefined;
1099
1125
  }, {
1100
1126
  projectId: string;
1127
+ minutes: number;
1101
1128
  location?: string | undefined;
1102
1129
  referrer?: string | undefined;
1103
- minutes?: number | undefined;
1104
1130
  }>;
1105
1131
  export type CurrentUsersRequest = z.infer<typeof currentUsersRequestSchema>;
1106
1132
  export declare const currentUsersResponseSchema: z.ZodObject<z.objectUtil.extendShape<{