saasco-sdk 0.1.8 → 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,9 +592,10 @@ 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(),
598
+ distinctId: userId,
596
599
  projectId: this.config.projectId,
597
600
  payload: JSON.stringify(Object.assign(Object.assign({}, browserContext), {
598
601
  properties: properties || {}
@@ -618,12 +621,9 @@ class Saasco {
618
621
  // When the user gets identified as null this will reset the users session and anonymous id
619
622
  // This should only be called when distinctId is null and there is an existing userId.
620
623
  // This means the user has logged out and we should reset the session and anonymous IDs
621
- const userIdChangedToNull = !distinctId && !!userId;
624
+ const userIdChangedToNull = distinctId === null && !!userId;
622
625
  if (userIdChangedToNull) this.log('User ID change to null. Session reset');
623
- // If we have a userId, but a new user ID is provided, we should reset the session and anonymous IDs
624
- const userIdChanged = !!userId && distinctId !== userId;
625
- if (userIdChanged) this.log('User ID changed. Session reset');
626
- const reset = userIdChangedToNull || userIdChanged;
626
+ const reset = userIdChangedToNull;
627
627
  setSessionId({
628
628
  reset
629
629
  });
@@ -640,7 +640,7 @@ class Saasco {
640
640
  projectId: this.config.projectId,
641
641
  distinctId,
642
642
  anonymousId,
643
- version: '2',
643
+ version,
644
644
  payload: properties || {},
645
645
  context: context || {}
646
646
  };
@@ -790,8 +790,15 @@ function coerceReservedProperties(properties) {
790
790
  }) => {
791
791
  const coercedValue = coerceValue(properties, coerceFrom, schema);
792
792
  if (coercedValue) acc[key] = coercedValue;
793
- if (!coercedValue && key === 'displayName' && acc['firstName']) {
794
- 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
+ }
795
802
  if (displayName) acc[key] = displayName;
796
803
  }
797
804
  return acc;
@@ -858,6 +865,25 @@ const TinybirdJobResponseSchema = zod.z.object({
858
865
  delete_id: zod.z.string().optional(),
859
866
  import_id: zod.z.string().optional()
860
867
  });
868
+ const latestEventsRequestSchema = zod.z.object({
869
+ projectId: zod.z.string(),
870
+ limit: zod.z.number().optional(),
871
+ payload: zod.z.boolean().optional()
872
+ });
873
+ const latestEventItemSchema = zod.z.object({
874
+ id: zod.z.string(),
875
+ timestamp: zod.z.string(),
876
+ action: zod.z.string(),
877
+ location: zod.z.string(),
878
+ referrer: zod.z.string(),
879
+ href: zod.z.string(),
880
+ device: zod.z.string(),
881
+ browser: zod.z.string(),
882
+ payload: zod.z.string().optional()
883
+ });
884
+ const latestEventsResponseSchema = tinyBirdBaseSchema.extend({
885
+ data: zod.z.array(latestEventItemSchema)
886
+ });
861
887
  const kpisRequestSchema = zod.z.object({
862
888
  projectId: zod.z.string(),
863
889
  dateTo: zod.z.string().optional(),
@@ -865,16 +891,17 @@ const kpisRequestSchema = zod.z.object({
865
891
  referrer: zod.z.string().optional(),
866
892
  location: zod.z.string().optional()
867
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
+ });
868
903
  const kpisResponseSchema = tinyBirdBaseSchema.extend({
869
- data: zod.z.array(zod.z.object({
870
- date: zod.z.string(),
871
- users: zod.z.number(),
872
- sessions: zod.z.number(),
873
- pageViews: zod.z.number(),
874
- events: zod.z.number(),
875
- bounceRate: zod.z.nullable(zod.z.number()),
876
- avgSessionSec: zod.z.number()
877
- }))
904
+ data: zod.z.array(kpiSchema)
878
905
  });
879
906
  const baseTopRequestSchema = zod.z.object({
880
907
  projectId: zod.z.string(),
@@ -944,6 +971,7 @@ const analyticsEventSchema = zod.z.object({
944
971
  projectId: zod.z.string(),
945
972
  anonymousId: zod.z.string(),
946
973
  sessionId: zod.z.string(),
974
+ distinctId: zod.z.string().nullable(),
947
975
  version: zod.z.string(),
948
976
  action: zod.z.string(),
949
977
  payload: zod.z.string()
@@ -953,7 +981,7 @@ const userEventsResponseSchema = tinyBirdBaseSchema.extend({
953
981
  });
954
982
  const currentUsersRequestSchema = zod.z.object({
955
983
  projectId: zod.z.string(),
956
- minutes: zod.z.number().optional(),
984
+ minutes: zod.z.number(),
957
985
  referrer: zod.z.string().optional(),
958
986
  location: zod.z.string().optional()
959
987
  });
@@ -1013,6 +1041,16 @@ const idenfitySchema = zod.z.object({
1013
1041
  active: zod.z.boolean().optional()
1014
1042
  }).optional()
1015
1043
  });
1044
+ const latestIdentifiesRequestSchema = zod.z.object({
1045
+ projectId: zod.z.string(),
1046
+ limit: zod.z.number().optional(),
1047
+ fields: zod.z.array(zod.z.string()).optional()
1048
+ });
1049
+ const latestIdentifiesResponseSchema = tinyBirdBaseSchema.extend({
1050
+ data: zod.z.array(zod.z.object({
1051
+ properties: contactPropertiesSchema
1052
+ }))
1053
+ });
1016
1054
 
1017
1055
  exports.Saasco = Saasco;
1018
1056
  exports.TinybirdJobResponseSchema = TinybirdJobResponseSchema;
@@ -1026,8 +1064,14 @@ exports.currentUsersRequestSchema = currentUsersRequestSchema;
1026
1064
  exports.currentUsersResponseSchema = currentUsersResponseSchema;
1027
1065
  exports.getAnalyticsUserParamsSchema = getAnalyticsUserParamsSchema;
1028
1066
  exports.idenfitySchema = idenfitySchema;
1067
+ exports.kpiSchema = kpiSchema;
1029
1068
  exports.kpisRequestSchema = kpisRequestSchema;
1030
1069
  exports.kpisResponseSchema = kpisResponseSchema;
1070
+ exports.latestEventItemSchema = latestEventItemSchema;
1071
+ exports.latestEventsRequestSchema = latestEventsRequestSchema;
1072
+ exports.latestEventsResponseSchema = latestEventsResponseSchema;
1073
+ exports.latestIdentifiesRequestSchema = latestIdentifiesRequestSchema;
1074
+ exports.latestIdentifiesResponseSchema = latestIdentifiesResponseSchema;
1031
1075
  exports.listUsersParamsSchema = listUsersParamsSchema;
1032
1076
  exports.reservedPropertiesSchema = reservedPropertiesSchema;
1033
1077
  exports.tbAny = tbAny;
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,9 +588,10 @@ 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(),
594
+ distinctId: userId,
592
595
  projectId: this.config.projectId,
593
596
  payload: JSON.stringify(Object.assign(Object.assign({}, browserContext), {
594
597
  properties: properties || {}
@@ -614,12 +617,9 @@ class Saasco {
614
617
  // When the user gets identified as null this will reset the users session and anonymous id
615
618
  // This should only be called when distinctId is null and there is an existing userId.
616
619
  // This means the user has logged out and we should reset the session and anonymous IDs
617
- const userIdChangedToNull = !distinctId && !!userId;
620
+ const userIdChangedToNull = distinctId === null && !!userId;
618
621
  if (userIdChangedToNull) this.log('User ID change to null. Session reset');
619
- // If we have a userId, but a new user ID is provided, we should reset the session and anonymous IDs
620
- const userIdChanged = !!userId && distinctId !== userId;
621
- if (userIdChanged) this.log('User ID changed. Session reset');
622
- const reset = userIdChangedToNull || userIdChanged;
622
+ const reset = userIdChangedToNull;
623
623
  setSessionId({
624
624
  reset
625
625
  });
@@ -636,7 +636,7 @@ class Saasco {
636
636
  projectId: this.config.projectId,
637
637
  distinctId,
638
638
  anonymousId,
639
- version: '2',
639
+ version,
640
640
  payload: properties || {},
641
641
  context: context || {}
642
642
  };
@@ -786,8 +786,15 @@ function coerceReservedProperties(properties) {
786
786
  }) => {
787
787
  const coercedValue = coerceValue(properties, coerceFrom, schema);
788
788
  if (coercedValue) acc[key] = coercedValue;
789
- if (!coercedValue && key === 'displayName' && acc['firstName']) {
790
- 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
+ }
791
798
  if (displayName) acc[key] = displayName;
792
799
  }
793
800
  return acc;
@@ -854,6 +861,25 @@ const TinybirdJobResponseSchema = z.object({
854
861
  delete_id: z.string().optional(),
855
862
  import_id: z.string().optional()
856
863
  });
864
+ const latestEventsRequestSchema = z.object({
865
+ projectId: z.string(),
866
+ limit: z.number().optional(),
867
+ payload: z.boolean().optional()
868
+ });
869
+ const latestEventItemSchema = z.object({
870
+ id: z.string(),
871
+ timestamp: z.string(),
872
+ action: z.string(),
873
+ location: z.string(),
874
+ referrer: z.string(),
875
+ href: z.string(),
876
+ device: z.string(),
877
+ browser: z.string(),
878
+ payload: z.string().optional()
879
+ });
880
+ const latestEventsResponseSchema = tinyBirdBaseSchema.extend({
881
+ data: z.array(latestEventItemSchema)
882
+ });
857
883
  const kpisRequestSchema = z.object({
858
884
  projectId: z.string(),
859
885
  dateTo: z.string().optional(),
@@ -861,16 +887,17 @@ const kpisRequestSchema = z.object({
861
887
  referrer: z.string().optional(),
862
888
  location: z.string().optional()
863
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
+ });
864
899
  const kpisResponseSchema = tinyBirdBaseSchema.extend({
865
- data: z.array(z.object({
866
- date: z.string(),
867
- users: z.number(),
868
- sessions: z.number(),
869
- pageViews: z.number(),
870
- events: z.number(),
871
- bounceRate: z.nullable(z.number()),
872
- avgSessionSec: z.number()
873
- }))
900
+ data: z.array(kpiSchema)
874
901
  });
875
902
  const baseTopRequestSchema = z.object({
876
903
  projectId: z.string(),
@@ -940,6 +967,7 @@ const analyticsEventSchema = z.object({
940
967
  projectId: z.string(),
941
968
  anonymousId: z.string(),
942
969
  sessionId: z.string(),
970
+ distinctId: z.string().nullable(),
943
971
  version: z.string(),
944
972
  action: z.string(),
945
973
  payload: z.string()
@@ -949,7 +977,7 @@ const userEventsResponseSchema = tinyBirdBaseSchema.extend({
949
977
  });
950
978
  const currentUsersRequestSchema = z.object({
951
979
  projectId: z.string(),
952
- minutes: z.number().optional(),
980
+ minutes: z.number(),
953
981
  referrer: z.string().optional(),
954
982
  location: z.string().optional()
955
983
  });
@@ -1009,5 +1037,15 @@ const idenfitySchema = z.object({
1009
1037
  active: z.boolean().optional()
1010
1038
  }).optional()
1011
1039
  });
1040
+ const latestIdentifiesRequestSchema = z.object({
1041
+ projectId: z.string(),
1042
+ limit: z.number().optional(),
1043
+ fields: z.array(z.string()).optional()
1044
+ });
1045
+ const latestIdentifiesResponseSchema = tinyBirdBaseSchema.extend({
1046
+ data: z.array(z.object({
1047
+ properties: contactPropertiesSchema
1048
+ }))
1049
+ });
1012
1050
 
1013
- export { Saasco, TinybirdJobResponseSchema, analyticsEventSchema, analyticsUserRawSchema, analyticsUserSchema, baseTopRequestSchema, coerceReservedProperties, contactPropertiesSchema, currentUsersRequestSchema, currentUsersResponseSchema, getAnalyticsUserParamsSchema, idenfitySchema, kpisRequestSchema, kpisResponseSchema, 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.8",
3
+ "version": "0.1.10",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "@lukeed/uuid": "^2.0.1",