saasco-sdk 0.1.9 → 0.1.10

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/index.cjs.js CHANGED
@@ -434,6 +434,8 @@ const timezones = {
434
434
  'Asia/Calcutta': 'IN'
435
435
  };
436
436
 
437
+ var version = "0.1.10";
438
+
437
439
  const isBrowser = typeof window !== 'undefined';
438
440
  let userId = null;
439
441
  const PREF = 'saasco-sdk';
@@ -590,7 +592,7 @@ class Saasco {
590
592
  id: uuid.v4(),
591
593
  timestamp: new Date().toISOString(),
592
594
  action: name,
593
- version: '2',
595
+ version,
594
596
  sessionId: getSessionId(),
595
597
  anonymousId: getAnonymousId(),
596
598
  distinctId: userId,
@@ -619,12 +621,9 @@ class Saasco {
619
621
  // When the user gets identified as null this will reset the users session and anonymous id
620
622
  // This should only be called when distinctId is null and there is an existing userId.
621
623
  // This means the user has logged out and we should reset the session and anonymous IDs
622
- const userIdChangedToNull = !distinctId && !!userId;
624
+ const userIdChangedToNull = distinctId === null && !!userId;
623
625
  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;
626
+ const reset = userIdChangedToNull;
628
627
  setSessionId({
629
628
  reset
630
629
  });
@@ -641,7 +640,7 @@ class Saasco {
641
640
  projectId: this.config.projectId,
642
641
  distinctId,
643
642
  anonymousId,
644
- version: '2',
643
+ version,
645
644
  payload: properties || {},
646
645
  context: context || {}
647
646
  };
@@ -791,8 +790,15 @@ function coerceReservedProperties(properties) {
791
790
  }) => {
792
791
  const coercedValue = coerceValue(properties, coerceFrom, schema);
793
792
  if (coercedValue) acc[key] = coercedValue;
794
- if (!coercedValue && key === 'displayName' && acc['firstName']) {
795
- const displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
793
+ if (!coercedValue && key === 'displayName') {
794
+ let displayName = '';
795
+ if (acc['firstName']) {
796
+ displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
797
+ } else if (acc['username'] && typeof acc['username'] === 'string') {
798
+ displayName = acc['username'];
799
+ } else if (acc['name'] && typeof acc['name'] === 'string') {
800
+ displayName = acc['name'];
801
+ }
796
802
  if (displayName) acc[key] = displayName;
797
803
  }
798
804
  return acc;
@@ -885,16 +891,17 @@ const kpisRequestSchema = zod.z.object({
885
891
  referrer: zod.z.string().optional(),
886
892
  location: zod.z.string().optional()
887
893
  });
894
+ const kpiSchema = zod.z.object({
895
+ date: zod.z.string(),
896
+ users: zod.z.number(),
897
+ sessions: zod.z.number(),
898
+ pageViews: zod.z.number(),
899
+ events: zod.z.number(),
900
+ bounceRate: zod.z.nullable(zod.z.number()),
901
+ avgSessionSec: zod.z.number()
902
+ });
888
903
  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
- }))
904
+ data: zod.z.array(kpiSchema)
898
905
  });
899
906
  const baseTopRequestSchema = zod.z.object({
900
907
  projectId: zod.z.string(),
@@ -964,7 +971,7 @@ const analyticsEventSchema = zod.z.object({
964
971
  projectId: zod.z.string(),
965
972
  anonymousId: zod.z.string(),
966
973
  sessionId: zod.z.string(),
967
- distinctId: zod.z.string().optional(),
974
+ distinctId: zod.z.string().nullable(),
968
975
  version: zod.z.string(),
969
976
  action: zod.z.string(),
970
977
  payload: zod.z.string()
@@ -974,7 +981,7 @@ const userEventsResponseSchema = tinyBirdBaseSchema.extend({
974
981
  });
975
982
  const currentUsersRequestSchema = zod.z.object({
976
983
  projectId: zod.z.string(),
977
- minutes: zod.z.number().optional(),
984
+ minutes: zod.z.number(),
978
985
  referrer: zod.z.string().optional(),
979
986
  location: zod.z.string().optional()
980
987
  });
@@ -1057,6 +1064,7 @@ exports.currentUsersRequestSchema = currentUsersRequestSchema;
1057
1064
  exports.currentUsersResponseSchema = currentUsersResponseSchema;
1058
1065
  exports.getAnalyticsUserParamsSchema = getAnalyticsUserParamsSchema;
1059
1066
  exports.idenfitySchema = idenfitySchema;
1067
+ exports.kpiSchema = kpiSchema;
1060
1068
  exports.kpisRequestSchema = kpisRequestSchema;
1061
1069
  exports.kpisResponseSchema = kpisResponseSchema;
1062
1070
  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.10";
434
+
433
435
  const isBrowser = typeof window !== 'undefined';
434
436
  let userId = null;
435
437
  const PREF = 'saasco-sdk';
@@ -586,7 +588,7 @@ class Saasco {
586
588
  id: v4(),
587
589
  timestamp: new Date().toISOString(),
588
590
  action: name,
589
- version: '2',
591
+ version,
590
592
  sessionId: getSessionId(),
591
593
  anonymousId: getAnonymousId(),
592
594
  distinctId: userId,
@@ -615,12 +617,9 @@ class Saasco {
615
617
  // When the user gets identified as null this will reset the users session and anonymous id
616
618
  // This should only be called when distinctId is null and there is an existing userId.
617
619
  // This means the user has logged out and we should reset the session and anonymous IDs
618
- const userIdChangedToNull = !distinctId && !!userId;
620
+ const userIdChangedToNull = distinctId === null && !!userId;
619
621
  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;
622
+ const reset = userIdChangedToNull;
624
623
  setSessionId({
625
624
  reset
626
625
  });
@@ -637,7 +636,7 @@ class Saasco {
637
636
  projectId: this.config.projectId,
638
637
  distinctId,
639
638
  anonymousId,
640
- version: '2',
639
+ version,
641
640
  payload: properties || {},
642
641
  context: context || {}
643
642
  };
@@ -787,8 +786,15 @@ function coerceReservedProperties(properties) {
787
786
  }) => {
788
787
  const coercedValue = coerceValue(properties, coerceFrom, schema);
789
788
  if (coercedValue) acc[key] = coercedValue;
790
- if (!coercedValue && key === 'displayName' && acc['firstName']) {
791
- const displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
789
+ if (!coercedValue && key === 'displayName') {
790
+ let displayName = '';
791
+ if (acc['firstName']) {
792
+ displayName = `${acc['firstName'] || ''} ${acc['lastName'] || ''}`.trim();
793
+ } else if (acc['username'] && typeof acc['username'] === 'string') {
794
+ displayName = acc['username'];
795
+ } else if (acc['name'] && typeof acc['name'] === 'string') {
796
+ displayName = acc['name'];
797
+ }
792
798
  if (displayName) acc[key] = displayName;
793
799
  }
794
800
  return acc;
@@ -881,16 +887,17 @@ const kpisRequestSchema = z.object({
881
887
  referrer: z.string().optional(),
882
888
  location: z.string().optional()
883
889
  });
890
+ const kpiSchema = z.object({
891
+ date: z.string(),
892
+ users: z.number(),
893
+ sessions: z.number(),
894
+ pageViews: z.number(),
895
+ events: z.number(),
896
+ bounceRate: z.nullable(z.number()),
897
+ avgSessionSec: z.number()
898
+ });
884
899
  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
- }))
900
+ data: z.array(kpiSchema)
894
901
  });
895
902
  const baseTopRequestSchema = z.object({
896
903
  projectId: z.string(),
@@ -960,7 +967,7 @@ const analyticsEventSchema = z.object({
960
967
  projectId: z.string(),
961
968
  anonymousId: z.string(),
962
969
  sessionId: z.string(),
963
- distinctId: z.string().optional(),
970
+ distinctId: z.string().nullable(),
964
971
  version: z.string(),
965
972
  action: z.string(),
966
973
  payload: z.string()
@@ -970,7 +977,7 @@ const userEventsResponseSchema = tinyBirdBaseSchema.extend({
970
977
  });
971
978
  const currentUsersRequestSchema = z.object({
972
979
  projectId: z.string(),
973
- minutes: z.number().optional(),
980
+ minutes: z.number(),
974
981
  referrer: z.string().optional(),
975
982
  location: z.string().optional()
976
983
  });
@@ -1041,4 +1048,4 @@ const latestIdentifiesResponseSchema = tinyBirdBaseSchema.extend({
1041
1048
  }))
1042
1049
  });
1043
1050
 
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 };
1051
+ 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.10",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "@lukeed/uuid": "^2.0.1",
@@ -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<{