saasco-sdk 0.1.16 → 0.1.17
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 +27 -9
- package/index.esm.js +27 -9
- package/package.json +1 -1
- package/src/lib/analytics.d.ts +9 -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.17";
|
|
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()}`;
|
|
@@ -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: false,
|
|
681
|
+
message: 'No distinctId provided'
|
|
682
|
+
});
|
|
678
683
|
const data = {
|
|
679
684
|
id: uuid.v4(),
|
|
680
685
|
timestamp: new Date().toISOString(),
|
|
@@ -701,7 +706,10 @@ class Saasco {
|
|
|
701
706
|
: data.action || path;
|
|
702
707
|
this.log(logMessage, data);
|
|
703
708
|
// If analytics is disabled, don't send the request
|
|
704
|
-
if (this.config.enabled === false) return
|
|
709
|
+
if (this.config.enabled === false) return {
|
|
710
|
+
success: false,
|
|
711
|
+
message: 'Analytics is disabled'
|
|
712
|
+
};
|
|
705
713
|
try {
|
|
706
714
|
const response = yield fetch(url, {
|
|
707
715
|
method: 'POST',
|
|
@@ -710,14 +718,20 @@ class Saasco {
|
|
|
710
718
|
},
|
|
711
719
|
body: JSON.stringify(data)
|
|
712
720
|
});
|
|
721
|
+
const responseBody = yield response.json();
|
|
713
722
|
if (!response.ok) {
|
|
714
|
-
|
|
715
|
-
throw new Error(`${errorBody.message}: ${errorBody.errors ? `[${errorBody.errors.join(', ')}]` : ''}` || JSON.stringify(errorBody));
|
|
723
|
+
throw new Error(`${responseBody.message}: ${responseBody.errors ? `[${responseBody.errors.join(', ')}]` : ''}` || JSON.stringify(responseBody));
|
|
716
724
|
}
|
|
717
|
-
return
|
|
725
|
+
return {
|
|
726
|
+
success: true,
|
|
727
|
+
message: responseBody.message
|
|
728
|
+
};
|
|
718
729
|
} catch (error) {
|
|
719
730
|
this.error(`\nError Message: "${error.message}"`, `\nPath: "/${path}"`, `\nRequest Data: ${JSON.stringify(data, null, 2)}`);
|
|
720
|
-
return
|
|
731
|
+
return {
|
|
732
|
+
success: false,
|
|
733
|
+
message: error.message
|
|
734
|
+
};
|
|
721
735
|
}
|
|
722
736
|
});
|
|
723
737
|
}
|
|
@@ -739,6 +753,10 @@ class Saasco {
|
|
|
739
753
|
console.error(...[`\x1b[41m\x1b[37m ${message} \x1b[0m`,
|
|
740
754
|
// Message highlighted for easy finding
|
|
741
755
|
...args]);
|
|
756
|
+
return {
|
|
757
|
+
success: false,
|
|
758
|
+
message: args.join(' ')
|
|
759
|
+
};
|
|
742
760
|
}
|
|
743
761
|
/**
|
|
744
762
|
* 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.17";
|
|
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()}`;
|
|
@@ -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: false,
|
|
677
|
+
message: 'No distinctId provided'
|
|
678
|
+
});
|
|
674
679
|
const data = {
|
|
675
680
|
id: v4(),
|
|
676
681
|
timestamp: new Date().toISOString(),
|
|
@@ -697,7 +702,10 @@ class Saasco {
|
|
|
697
702
|
: data.action || path;
|
|
698
703
|
this.log(logMessage, data);
|
|
699
704
|
// If analytics is disabled, don't send the request
|
|
700
|
-
if (this.config.enabled === false) return
|
|
705
|
+
if (this.config.enabled === false) return {
|
|
706
|
+
success: false,
|
|
707
|
+
message: 'Analytics is disabled'
|
|
708
|
+
};
|
|
701
709
|
try {
|
|
702
710
|
const response = yield fetch(url, {
|
|
703
711
|
method: 'POST',
|
|
@@ -706,14 +714,20 @@ class Saasco {
|
|
|
706
714
|
},
|
|
707
715
|
body: JSON.stringify(data)
|
|
708
716
|
});
|
|
717
|
+
const responseBody = yield response.json();
|
|
709
718
|
if (!response.ok) {
|
|
710
|
-
|
|
711
|
-
throw new Error(`${errorBody.message}: ${errorBody.errors ? `[${errorBody.errors.join(', ')}]` : ''}` || JSON.stringify(errorBody));
|
|
719
|
+
throw new Error(`${responseBody.message}: ${responseBody.errors ? `[${responseBody.errors.join(', ')}]` : ''}` || JSON.stringify(responseBody));
|
|
712
720
|
}
|
|
713
|
-
return
|
|
721
|
+
return {
|
|
722
|
+
success: true,
|
|
723
|
+
message: responseBody.message
|
|
724
|
+
};
|
|
714
725
|
} catch (error) {
|
|
715
726
|
this.error(`\nError Message: "${error.message}"`, `\nPath: "/${path}"`, `\nRequest Data: ${JSON.stringify(data, null, 2)}`);
|
|
716
|
-
return
|
|
727
|
+
return {
|
|
728
|
+
success: false,
|
|
729
|
+
message: error.message
|
|
730
|
+
};
|
|
717
731
|
}
|
|
718
732
|
});
|
|
719
733
|
}
|
|
@@ -735,6 +749,10 @@ class Saasco {
|
|
|
735
749
|
console.error(...[`\x1b[41m\x1b[37m ${message} \x1b[0m`,
|
|
736
750
|
// Message highlighted for easy finding
|
|
737
751
|
...args]);
|
|
752
|
+
return {
|
|
753
|
+
success: false,
|
|
754
|
+
message: args.join(' ')
|
|
755
|
+
};
|
|
738
756
|
}
|
|
739
757
|
/**
|
|
740
758
|
* 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,10 @@ 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>;
|
|
70
74
|
/**
|
|
71
75
|
* Handles sending data to the Saasco API
|
|
72
76
|
* 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 +93,4 @@ export declare class Saasco {
|
|
|
89
93
|
*/
|
|
90
94
|
private initiAutoPageTracking;
|
|
91
95
|
}
|
|
96
|
+
export {};
|