saasco-sdk 0.1.16 → 0.1.18
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 +38 -10
- package/index.esm.js +38 -10
- package/package.json +1 -1
- package/src/lib/analytics.d.ts +17 -4
package/index.cjs.js
CHANGED
|
@@ -6,7 +6,7 @@ var tslib = require('tslib');
|
|
|
6
6
|
var uuid = require('@lukeed/uuid');
|
|
7
7
|
var zod = require('zod');
|
|
8
8
|
|
|
9
|
-
var version = "0.1.
|
|
9
|
+
var version = "0.1.18";
|
|
10
10
|
|
|
11
11
|
const timezones = {
|
|
12
12
|
'Asia/Barnaul': 'RU',
|
|
@@ -603,7 +603,8 @@ class Saasco {
|
|
|
603
603
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
604
604
|
properties, context) {
|
|
605
605
|
if (!this.config.projectId) {
|
|
606
|
-
|
|
606
|
+
const response = this.error("Unable to track event. Project ID is required but has not been provided. If you are using an env variable make sure it's set correctly.");
|
|
607
|
+
return Promise.resolve(response);
|
|
607
608
|
}
|
|
608
609
|
setSessionId();
|
|
609
610
|
setAnonmousId();
|
|
@@ -653,7 +654,8 @@ class Saasco {
|
|
|
653
654
|
}
|
|
654
655
|
identify(distinctIdOrProperties, propertiesOrContext, contextOrNothing) {
|
|
655
656
|
if (!this.config.projectId) {
|
|
656
|
-
|
|
657
|
+
const response = this.error("Unable to identify user. Project ID is required but has not been provided. If you are using an env variable make sure it's set correctly.");
|
|
658
|
+
return Promise.resolve(response);
|
|
657
659
|
}
|
|
658
660
|
const hasId = typeof distinctIdOrProperties === 'string' || typeof distinctIdOrProperties === 'number' || distinctIdOrProperties === null;
|
|
659
661
|
const distinctId = hasId ? distinctIdOrProperties === null || distinctIdOrProperties === void 0 ? void 0 : distinctIdOrProperties.toString() : `soft_${uuid.v4()}`;
|
|
@@ -663,7 +665,7 @@ class Saasco {
|
|
|
663
665
|
// This should only be called when distinctId is null and there is an existing userId.
|
|
664
666
|
// This means the user has logged out and we should reset the session and anonymous IDs
|
|
665
667
|
const userIdChangedToNull = distinctId === null && !!getUserId();
|
|
666
|
-
if (userIdChangedToNull) this.log('User
|
|
668
|
+
if (userIdChangedToNull) this.log('User logged out');
|
|
667
669
|
const reset = userIdChangedToNull;
|
|
668
670
|
setSessionId({
|
|
669
671
|
reset
|
|
@@ -674,7 +676,10 @@ class Saasco {
|
|
|
674
676
|
// set the distinct Id to the userId
|
|
675
677
|
setUserId(distinctId);
|
|
676
678
|
// No distinctId provided so we don't track the user
|
|
677
|
-
if (!distinctId) return
|
|
679
|
+
if (!distinctId) return Promise.resolve({
|
|
680
|
+
success: true,
|
|
681
|
+
message: 'No distinctId provided'
|
|
682
|
+
});
|
|
678
683
|
const data = {
|
|
679
684
|
id: uuid.v4(),
|
|
680
685
|
timestamp: new Date().toISOString(),
|
|
@@ -687,6 +692,16 @@ class Saasco {
|
|
|
687
692
|
};
|
|
688
693
|
return this.doRequest('identify', data);
|
|
689
694
|
}
|
|
695
|
+
/**
|
|
696
|
+
* This should only be called when the user logs out
|
|
697
|
+
* It will reset the session, anonymous id, and user id
|
|
698
|
+
*
|
|
699
|
+
* All events after calling reset will be tracked as a new user
|
|
700
|
+
*
|
|
701
|
+
*/
|
|
702
|
+
logout() {
|
|
703
|
+
this.identify(null);
|
|
704
|
+
}
|
|
690
705
|
/**
|
|
691
706
|
* Handles sending data to the Saasco API
|
|
692
707
|
* 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
|
|
@@ -701,7 +716,10 @@ class Saasco {
|
|
|
701
716
|
: data.action || path;
|
|
702
717
|
this.log(logMessage, data);
|
|
703
718
|
// If analytics is disabled, don't send the request
|
|
704
|
-
if (this.config.enabled === false) return
|
|
719
|
+
if (this.config.enabled === false) return {
|
|
720
|
+
success: false,
|
|
721
|
+
message: 'Analytics is disabled'
|
|
722
|
+
};
|
|
705
723
|
try {
|
|
706
724
|
const response = yield fetch(url, {
|
|
707
725
|
method: 'POST',
|
|
@@ -710,14 +728,20 @@ class Saasco {
|
|
|
710
728
|
},
|
|
711
729
|
body: JSON.stringify(data)
|
|
712
730
|
});
|
|
731
|
+
const responseBody = yield response.json();
|
|
713
732
|
if (!response.ok) {
|
|
714
|
-
|
|
715
|
-
throw new Error(`${errorBody.message}: ${errorBody.errors ? `[${errorBody.errors.join(', ')}]` : ''}` || JSON.stringify(errorBody));
|
|
733
|
+
throw new Error(`${responseBody.message}: ${responseBody.errors ? `[${responseBody.errors.join(', ')}]` : ''}` || JSON.stringify(responseBody));
|
|
716
734
|
}
|
|
717
|
-
return
|
|
735
|
+
return {
|
|
736
|
+
success: true,
|
|
737
|
+
message: responseBody.message
|
|
738
|
+
};
|
|
718
739
|
} catch (error) {
|
|
719
740
|
this.error(`\nError Message: "${error.message}"`, `\nPath: "/${path}"`, `\nRequest Data: ${JSON.stringify(data, null, 2)}`);
|
|
720
|
-
return
|
|
741
|
+
return {
|
|
742
|
+
success: false,
|
|
743
|
+
message: error.message
|
|
744
|
+
};
|
|
721
745
|
}
|
|
722
746
|
});
|
|
723
747
|
}
|
|
@@ -739,6 +763,10 @@ class Saasco {
|
|
|
739
763
|
console.error(...[`\x1b[41m\x1b[37m ${message} \x1b[0m`,
|
|
740
764
|
// Message highlighted for easy finding
|
|
741
765
|
...args]);
|
|
766
|
+
return {
|
|
767
|
+
success: false,
|
|
768
|
+
message: args.join(' ')
|
|
769
|
+
};
|
|
742
770
|
}
|
|
743
771
|
/**
|
|
744
772
|
* If autoPageTracking is enabled, this will automatically track page views
|
package/index.esm.js
CHANGED
|
@@ -2,7 +2,7 @@ import { __awaiter } from 'tslib';
|
|
|
2
2
|
import { v4 } from '@lukeed/uuid';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
|
-
var version = "0.1.
|
|
5
|
+
var version = "0.1.18";
|
|
6
6
|
|
|
7
7
|
const timezones = {
|
|
8
8
|
'Asia/Barnaul': 'RU',
|
|
@@ -599,7 +599,8 @@ class Saasco {
|
|
|
599
599
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
600
600
|
properties, context) {
|
|
601
601
|
if (!this.config.projectId) {
|
|
602
|
-
|
|
602
|
+
const response = this.error("Unable to track event. Project ID is required but has not been provided. If you are using an env variable make sure it's set correctly.");
|
|
603
|
+
return Promise.resolve(response);
|
|
603
604
|
}
|
|
604
605
|
setSessionId();
|
|
605
606
|
setAnonmousId();
|
|
@@ -649,7 +650,8 @@ class Saasco {
|
|
|
649
650
|
}
|
|
650
651
|
identify(distinctIdOrProperties, propertiesOrContext, contextOrNothing) {
|
|
651
652
|
if (!this.config.projectId) {
|
|
652
|
-
|
|
653
|
+
const response = this.error("Unable to identify user. Project ID is required but has not been provided. If you are using an env variable make sure it's set correctly.");
|
|
654
|
+
return Promise.resolve(response);
|
|
653
655
|
}
|
|
654
656
|
const hasId = typeof distinctIdOrProperties === 'string' || typeof distinctIdOrProperties === 'number' || distinctIdOrProperties === null;
|
|
655
657
|
const distinctId = hasId ? distinctIdOrProperties === null || distinctIdOrProperties === void 0 ? void 0 : distinctIdOrProperties.toString() : `soft_${v4()}`;
|
|
@@ -659,7 +661,7 @@ class Saasco {
|
|
|
659
661
|
// This should only be called when distinctId is null and there is an existing userId.
|
|
660
662
|
// This means the user has logged out and we should reset the session and anonymous IDs
|
|
661
663
|
const userIdChangedToNull = distinctId === null && !!getUserId();
|
|
662
|
-
if (userIdChangedToNull) this.log('User
|
|
664
|
+
if (userIdChangedToNull) this.log('User logged out');
|
|
663
665
|
const reset = userIdChangedToNull;
|
|
664
666
|
setSessionId({
|
|
665
667
|
reset
|
|
@@ -670,7 +672,10 @@ class Saasco {
|
|
|
670
672
|
// set the distinct Id to the userId
|
|
671
673
|
setUserId(distinctId);
|
|
672
674
|
// No distinctId provided so we don't track the user
|
|
673
|
-
if (!distinctId) return
|
|
675
|
+
if (!distinctId) return Promise.resolve({
|
|
676
|
+
success: true,
|
|
677
|
+
message: 'No distinctId provided'
|
|
678
|
+
});
|
|
674
679
|
const data = {
|
|
675
680
|
id: v4(),
|
|
676
681
|
timestamp: new Date().toISOString(),
|
|
@@ -683,6 +688,16 @@ class Saasco {
|
|
|
683
688
|
};
|
|
684
689
|
return this.doRequest('identify', data);
|
|
685
690
|
}
|
|
691
|
+
/**
|
|
692
|
+
* This should only be called when the user logs out
|
|
693
|
+
* It will reset the session, anonymous id, and user id
|
|
694
|
+
*
|
|
695
|
+
* All events after calling reset will be tracked as a new user
|
|
696
|
+
*
|
|
697
|
+
*/
|
|
698
|
+
logout() {
|
|
699
|
+
this.identify(null);
|
|
700
|
+
}
|
|
686
701
|
/**
|
|
687
702
|
* Handles sending data to the Saasco API
|
|
688
703
|
* 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
|
|
@@ -697,7 +712,10 @@ class Saasco {
|
|
|
697
712
|
: data.action || path;
|
|
698
713
|
this.log(logMessage, data);
|
|
699
714
|
// If analytics is disabled, don't send the request
|
|
700
|
-
if (this.config.enabled === false) return
|
|
715
|
+
if (this.config.enabled === false) return {
|
|
716
|
+
success: false,
|
|
717
|
+
message: 'Analytics is disabled'
|
|
718
|
+
};
|
|
701
719
|
try {
|
|
702
720
|
const response = yield fetch(url, {
|
|
703
721
|
method: 'POST',
|
|
@@ -706,14 +724,20 @@ class Saasco {
|
|
|
706
724
|
},
|
|
707
725
|
body: JSON.stringify(data)
|
|
708
726
|
});
|
|
727
|
+
const responseBody = yield response.json();
|
|
709
728
|
if (!response.ok) {
|
|
710
|
-
|
|
711
|
-
throw new Error(`${errorBody.message}: ${errorBody.errors ? `[${errorBody.errors.join(', ')}]` : ''}` || JSON.stringify(errorBody));
|
|
729
|
+
throw new Error(`${responseBody.message}: ${responseBody.errors ? `[${responseBody.errors.join(', ')}]` : ''}` || JSON.stringify(responseBody));
|
|
712
730
|
}
|
|
713
|
-
return
|
|
731
|
+
return {
|
|
732
|
+
success: true,
|
|
733
|
+
message: responseBody.message
|
|
734
|
+
};
|
|
714
735
|
} catch (error) {
|
|
715
736
|
this.error(`\nError Message: "${error.message}"`, `\nPath: "/${path}"`, `\nRequest Data: ${JSON.stringify(data, null, 2)}`);
|
|
716
|
-
return
|
|
737
|
+
return {
|
|
738
|
+
success: false,
|
|
739
|
+
message: error.message
|
|
740
|
+
};
|
|
717
741
|
}
|
|
718
742
|
});
|
|
719
743
|
}
|
|
@@ -735,6 +759,10 @@ class Saasco {
|
|
|
735
759
|
console.error(...[`\x1b[41m\x1b[37m ${message} \x1b[0m`,
|
|
736
760
|
// Message highlighted for easy finding
|
|
737
761
|
...args]);
|
|
762
|
+
return {
|
|
763
|
+
success: false,
|
|
764
|
+
message: args.join(' ')
|
|
765
|
+
};
|
|
738
766
|
}
|
|
739
767
|
/**
|
|
740
768
|
* If autoPageTracking is enabled, this will automatically track page views
|
package/package.json
CHANGED
package/src/lib/analytics.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ declare global {
|
|
|
4
4
|
saasco: Saasco;
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
|
+
type DoRequestResponse = {
|
|
8
|
+
success: boolean;
|
|
9
|
+
message: string;
|
|
10
|
+
};
|
|
7
11
|
export declare class Saasco {
|
|
8
12
|
private config;
|
|
9
13
|
private lastPageViewPath;
|
|
@@ -44,14 +48,14 @@ export declare class Saasco {
|
|
|
44
48
|
*/
|
|
45
49
|
track(name: string, properties?: Record<string, any>, context?: {
|
|
46
50
|
active?: boolean;
|
|
47
|
-
}):
|
|
51
|
+
}): Promise<DoRequestResponse>;
|
|
48
52
|
/**
|
|
49
53
|
* The page method lets you record page views on your website
|
|
50
54
|
* This records the page title and path and names the event useing the reserved property "Page Viewed"
|
|
51
55
|
*
|
|
52
56
|
* Before implementing this make sure you have disabled the autoPageTracking in the config or you will get duplicate page views
|
|
53
57
|
*/
|
|
54
|
-
page():
|
|
58
|
+
page(): Promise<DoRequestResponse> | undefined;
|
|
55
59
|
/**
|
|
56
60
|
* The identify method lets you tie a user to their actions and record traits about them.
|
|
57
61
|
* We recommend you call this when the user logs in and when any traits get updated.
|
|
@@ -63,10 +67,18 @@ export declare class Saasco {
|
|
|
63
67
|
*/
|
|
64
68
|
identify(properties: Record<string, any>, context?: {
|
|
65
69
|
active?: boolean;
|
|
66
|
-
}):
|
|
70
|
+
}): Promise<DoRequestResponse>;
|
|
67
71
|
identify(distinctId: string | number | null, properties?: Record<string, any>, context?: {
|
|
68
72
|
active?: boolean;
|
|
69
|
-
}):
|
|
73
|
+
}): Promise<DoRequestResponse>;
|
|
74
|
+
/**
|
|
75
|
+
* This should only be called when the user logs out
|
|
76
|
+
* It will reset the session, anonymous id, and user id
|
|
77
|
+
*
|
|
78
|
+
* All events after calling reset will be tracked as a new user
|
|
79
|
+
*
|
|
80
|
+
*/
|
|
81
|
+
logout(): void;
|
|
70
82
|
/**
|
|
71
83
|
* Handles sending data to the Saasco API
|
|
72
84
|
* 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
|
|
@@ -89,3 +101,4 @@ export declare class Saasco {
|
|
|
89
101
|
*/
|
|
90
102
|
private initiAutoPageTracking;
|
|
91
103
|
}
|
|
104
|
+
export {};
|