saasco-sdk 0.1.15 → 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/README.md +14 -13
- package/index.cjs.js +42 -18
- package/index.esm.js +42 -18
- package/package.json +1 -1
- package/src/lib/analytics.d.ts +14 -7
package/README.md
CHANGED
|
@@ -35,22 +35,23 @@ export const saasco = new Saasco({ projectId: 'YOUR-PROJECT-ID' });
|
|
|
35
35
|
|
|
36
36
|
## Automatic Page View Tracking
|
|
37
37
|
|
|
38
|
-
When you initiate the lib Saasco will automatically start tracking all page views in your app. By default, it tracks all URL changes, including query parameters and hash changes. You can customize this behavior using the configuration
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
When you initiate the lib Saasco will automatically start tracking all page views in your app. By default, it tracks all URL changes, including query parameters and hash changes. You can customize this behavior using the `autoPageTracking` configuration object:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
const analytics = new Saasco({
|
|
42
|
+
projectId: 'your-project-id',
|
|
43
|
+
autoPageTracking: {
|
|
44
|
+
enabled: true, // Set to false to disable automatic page tracking
|
|
45
|
+
trackQueryParams: true, // Set to false to ignore URL query parameter changes
|
|
46
|
+
trackHash: true, // Set to false to ignore URL hash changes
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
saasco.init();
|
|
50
|
+
analytics.init();
|
|
52
51
|
```
|
|
53
52
|
|
|
53
|
+
You can also [manually track pages](https://www.notion.so/Manual-Page-Tracking-in-SPAs-2442fc7586dc4208ae8f669eb7561b1a?pvs=21) by disabling automatic page tracking (`enabled: false`). For most use cases you don't need to do this.
|
|
54
|
+
|
|
54
55
|
## Tracking Events
|
|
55
56
|
|
|
56
57
|
With events you can track custom actions users are taking on your site with event properties.
|
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',
|
|
@@ -562,9 +562,12 @@ class Saasco {
|
|
|
562
562
|
}
|
|
563
563
|
// default enabled to true
|
|
564
564
|
this.config.enabled = this.config.enabled === undefined ? true : this.config.enabled;
|
|
565
|
-
// default
|
|
566
|
-
this.config.
|
|
567
|
-
|
|
565
|
+
// Set default auto page tracking config
|
|
566
|
+
this.config.autoPageTracking = Object.assign({
|
|
567
|
+
enabled: true,
|
|
568
|
+
trackQueryParams: true,
|
|
569
|
+
trackHash: true
|
|
570
|
+
}, this.config.autoPageTracking);
|
|
568
571
|
}
|
|
569
572
|
init() {
|
|
570
573
|
if (this.isInitialized) {
|
|
@@ -600,7 +603,8 @@ class Saasco {
|
|
|
600
603
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
601
604
|
properties, context) {
|
|
602
605
|
if (!this.config.projectId) {
|
|
603
|
-
|
|
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);
|
|
604
608
|
}
|
|
605
609
|
setSessionId();
|
|
606
610
|
setAnonmousId();
|
|
@@ -628,6 +632,7 @@ class Saasco {
|
|
|
628
632
|
* Before implementing this make sure you have disabled the autoPageTracking in the config or you will get duplicate page views
|
|
629
633
|
*/
|
|
630
634
|
page() {
|
|
635
|
+
var _a, _b;
|
|
631
636
|
if (!isBrowser) {
|
|
632
637
|
console.warn('Saasco page tracking is only available in the browser');
|
|
633
638
|
return;
|
|
@@ -635,11 +640,11 @@ class Saasco {
|
|
|
635
640
|
const url = new URL(window.location.href);
|
|
636
641
|
let pathToCompare = url.pathname;
|
|
637
642
|
// Include search params if configured
|
|
638
|
-
if (this.config.
|
|
643
|
+
if ((_a = this.config.autoPageTracking) === null || _a === void 0 ? void 0 : _a.trackQueryParams) {
|
|
639
644
|
pathToCompare += url.search;
|
|
640
645
|
}
|
|
641
646
|
// Include hash if configured
|
|
642
|
-
if (this.config.
|
|
647
|
+
if ((_b = this.config.autoPageTracking) === null || _b === void 0 ? void 0 : _b.trackHash) {
|
|
643
648
|
pathToCompare += url.hash;
|
|
644
649
|
}
|
|
645
650
|
// Only track if the path has changed
|
|
@@ -649,7 +654,8 @@ class Saasco {
|
|
|
649
654
|
}
|
|
650
655
|
identify(distinctIdOrProperties, propertiesOrContext, contextOrNothing) {
|
|
651
656
|
if (!this.config.projectId) {
|
|
652
|
-
|
|
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);
|
|
653
659
|
}
|
|
654
660
|
const hasId = typeof distinctIdOrProperties === 'string' || typeof distinctIdOrProperties === 'number' || distinctIdOrProperties === null;
|
|
655
661
|
const distinctId = hasId ? distinctIdOrProperties === null || distinctIdOrProperties === void 0 ? void 0 : distinctIdOrProperties.toString() : `soft_${uuid.v4()}`;
|
|
@@ -670,7 +676,10 @@ class Saasco {
|
|
|
670
676
|
// set the distinct Id to the userId
|
|
671
677
|
setUserId(distinctId);
|
|
672
678
|
// No distinctId provided so we don't track the user
|
|
673
|
-
if (!distinctId) return
|
|
679
|
+
if (!distinctId) return Promise.resolve({
|
|
680
|
+
success: false,
|
|
681
|
+
message: 'No distinctId provided'
|
|
682
|
+
});
|
|
674
683
|
const data = {
|
|
675
684
|
id: uuid.v4(),
|
|
676
685
|
timestamp: new Date().toISOString(),
|
|
@@ -697,7 +706,10 @@ class Saasco {
|
|
|
697
706
|
: data.action || path;
|
|
698
707
|
this.log(logMessage, data);
|
|
699
708
|
// If analytics is disabled, don't send the request
|
|
700
|
-
if (this.config.enabled === false) return
|
|
709
|
+
if (this.config.enabled === false) return {
|
|
710
|
+
success: false,
|
|
711
|
+
message: 'Analytics is disabled'
|
|
712
|
+
};
|
|
701
713
|
try {
|
|
702
714
|
const response = yield fetch(url, {
|
|
703
715
|
method: 'POST',
|
|
@@ -706,14 +718,20 @@ class Saasco {
|
|
|
706
718
|
},
|
|
707
719
|
body: JSON.stringify(data)
|
|
708
720
|
});
|
|
721
|
+
const responseBody = yield response.json();
|
|
709
722
|
if (!response.ok) {
|
|
710
|
-
|
|
711
|
-
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));
|
|
712
724
|
}
|
|
713
|
-
return
|
|
725
|
+
return {
|
|
726
|
+
success: true,
|
|
727
|
+
message: responseBody.message
|
|
728
|
+
};
|
|
714
729
|
} catch (error) {
|
|
715
730
|
this.error(`\nError Message: "${error.message}"`, `\nPath: "/${path}"`, `\nRequest Data: ${JSON.stringify(data, null, 2)}`);
|
|
716
|
-
return
|
|
731
|
+
return {
|
|
732
|
+
success: false,
|
|
733
|
+
message: error.message
|
|
734
|
+
};
|
|
717
735
|
}
|
|
718
736
|
});
|
|
719
737
|
}
|
|
@@ -735,6 +753,10 @@ class Saasco {
|
|
|
735
753
|
console.error(...[`\x1b[41m\x1b[37m ${message} \x1b[0m`,
|
|
736
754
|
// Message highlighted for easy finding
|
|
737
755
|
...args]);
|
|
756
|
+
return {
|
|
757
|
+
success: false,
|
|
758
|
+
message: args.join(' ')
|
|
759
|
+
};
|
|
738
760
|
}
|
|
739
761
|
/**
|
|
740
762
|
* If autoPageTracking is enabled, this will automatically track page views
|
|
@@ -742,9 +764,9 @@ class Saasco {
|
|
|
742
764
|
* @returns
|
|
743
765
|
*/
|
|
744
766
|
initiAutoPageTracking() {
|
|
767
|
+
var _a, _b;
|
|
745
768
|
// Disable auto page tracking if the config is set to false
|
|
746
|
-
|
|
747
|
-
if (this.config.autoPageTracking === false) return;
|
|
769
|
+
if (!((_a = this.config.autoPageTracking) === null || _a === void 0 ? void 0 : _a.enabled)) return;
|
|
748
770
|
// Prevent running on the server
|
|
749
771
|
if (!isBrowser) return console.warn('Saasco auto page tracking is only available in the browser');
|
|
750
772
|
// Prevent intitializing auto page tracking more than once
|
|
@@ -756,8 +778,10 @@ class Saasco {
|
|
|
756
778
|
this.log('Auto Page Tracking enabled');
|
|
757
779
|
// Track initial page load
|
|
758
780
|
this.page();
|
|
759
|
-
// Listen for hash changes
|
|
760
|
-
|
|
781
|
+
// Listen for hash changes if hash tracking is enabled
|
|
782
|
+
if ((_b = this.config.autoPageTracking) === null || _b === void 0 ? void 0 : _b.trackHash) {
|
|
783
|
+
window.addEventListener('hashchange', () => this.page());
|
|
784
|
+
}
|
|
761
785
|
// Listen to popstate for back and forward navigation
|
|
762
786
|
window.addEventListener('popstate', () => this.page());
|
|
763
787
|
// Wrap history push state to listen for URL changes
|
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',
|
|
@@ -558,9 +558,12 @@ class Saasco {
|
|
|
558
558
|
}
|
|
559
559
|
// default enabled to true
|
|
560
560
|
this.config.enabled = this.config.enabled === undefined ? true : this.config.enabled;
|
|
561
|
-
// default
|
|
562
|
-
this.config.
|
|
563
|
-
|
|
561
|
+
// Set default auto page tracking config
|
|
562
|
+
this.config.autoPageTracking = Object.assign({
|
|
563
|
+
enabled: true,
|
|
564
|
+
trackQueryParams: true,
|
|
565
|
+
trackHash: true
|
|
566
|
+
}, this.config.autoPageTracking);
|
|
564
567
|
}
|
|
565
568
|
init() {
|
|
566
569
|
if (this.isInitialized) {
|
|
@@ -596,7 +599,8 @@ class Saasco {
|
|
|
596
599
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
597
600
|
properties, context) {
|
|
598
601
|
if (!this.config.projectId) {
|
|
599
|
-
|
|
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);
|
|
600
604
|
}
|
|
601
605
|
setSessionId();
|
|
602
606
|
setAnonmousId();
|
|
@@ -624,6 +628,7 @@ class Saasco {
|
|
|
624
628
|
* Before implementing this make sure you have disabled the autoPageTracking in the config or you will get duplicate page views
|
|
625
629
|
*/
|
|
626
630
|
page() {
|
|
631
|
+
var _a, _b;
|
|
627
632
|
if (!isBrowser) {
|
|
628
633
|
console.warn('Saasco page tracking is only available in the browser');
|
|
629
634
|
return;
|
|
@@ -631,11 +636,11 @@ class Saasco {
|
|
|
631
636
|
const url = new URL(window.location.href);
|
|
632
637
|
let pathToCompare = url.pathname;
|
|
633
638
|
// Include search params if configured
|
|
634
|
-
if (this.config.
|
|
639
|
+
if ((_a = this.config.autoPageTracking) === null || _a === void 0 ? void 0 : _a.trackQueryParams) {
|
|
635
640
|
pathToCompare += url.search;
|
|
636
641
|
}
|
|
637
642
|
// Include hash if configured
|
|
638
|
-
if (this.config.
|
|
643
|
+
if ((_b = this.config.autoPageTracking) === null || _b === void 0 ? void 0 : _b.trackHash) {
|
|
639
644
|
pathToCompare += url.hash;
|
|
640
645
|
}
|
|
641
646
|
// Only track if the path has changed
|
|
@@ -645,7 +650,8 @@ class Saasco {
|
|
|
645
650
|
}
|
|
646
651
|
identify(distinctIdOrProperties, propertiesOrContext, contextOrNothing) {
|
|
647
652
|
if (!this.config.projectId) {
|
|
648
|
-
|
|
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);
|
|
649
655
|
}
|
|
650
656
|
const hasId = typeof distinctIdOrProperties === 'string' || typeof distinctIdOrProperties === 'number' || distinctIdOrProperties === null;
|
|
651
657
|
const distinctId = hasId ? distinctIdOrProperties === null || distinctIdOrProperties === void 0 ? void 0 : distinctIdOrProperties.toString() : `soft_${v4()}`;
|
|
@@ -666,7 +672,10 @@ class Saasco {
|
|
|
666
672
|
// set the distinct Id to the userId
|
|
667
673
|
setUserId(distinctId);
|
|
668
674
|
// No distinctId provided so we don't track the user
|
|
669
|
-
if (!distinctId) return
|
|
675
|
+
if (!distinctId) return Promise.resolve({
|
|
676
|
+
success: false,
|
|
677
|
+
message: 'No distinctId provided'
|
|
678
|
+
});
|
|
670
679
|
const data = {
|
|
671
680
|
id: v4(),
|
|
672
681
|
timestamp: new Date().toISOString(),
|
|
@@ -693,7 +702,10 @@ class Saasco {
|
|
|
693
702
|
: data.action || path;
|
|
694
703
|
this.log(logMessage, data);
|
|
695
704
|
// If analytics is disabled, don't send the request
|
|
696
|
-
if (this.config.enabled === false) return
|
|
705
|
+
if (this.config.enabled === false) return {
|
|
706
|
+
success: false,
|
|
707
|
+
message: 'Analytics is disabled'
|
|
708
|
+
};
|
|
697
709
|
try {
|
|
698
710
|
const response = yield fetch(url, {
|
|
699
711
|
method: 'POST',
|
|
@@ -702,14 +714,20 @@ class Saasco {
|
|
|
702
714
|
},
|
|
703
715
|
body: JSON.stringify(data)
|
|
704
716
|
});
|
|
717
|
+
const responseBody = yield response.json();
|
|
705
718
|
if (!response.ok) {
|
|
706
|
-
|
|
707
|
-
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));
|
|
708
720
|
}
|
|
709
|
-
return
|
|
721
|
+
return {
|
|
722
|
+
success: true,
|
|
723
|
+
message: responseBody.message
|
|
724
|
+
};
|
|
710
725
|
} catch (error) {
|
|
711
726
|
this.error(`\nError Message: "${error.message}"`, `\nPath: "/${path}"`, `\nRequest Data: ${JSON.stringify(data, null, 2)}`);
|
|
712
|
-
return
|
|
727
|
+
return {
|
|
728
|
+
success: false,
|
|
729
|
+
message: error.message
|
|
730
|
+
};
|
|
713
731
|
}
|
|
714
732
|
});
|
|
715
733
|
}
|
|
@@ -731,6 +749,10 @@ class Saasco {
|
|
|
731
749
|
console.error(...[`\x1b[41m\x1b[37m ${message} \x1b[0m`,
|
|
732
750
|
// Message highlighted for easy finding
|
|
733
751
|
...args]);
|
|
752
|
+
return {
|
|
753
|
+
success: false,
|
|
754
|
+
message: args.join(' ')
|
|
755
|
+
};
|
|
734
756
|
}
|
|
735
757
|
/**
|
|
736
758
|
* If autoPageTracking is enabled, this will automatically track page views
|
|
@@ -738,9 +760,9 @@ class Saasco {
|
|
|
738
760
|
* @returns
|
|
739
761
|
*/
|
|
740
762
|
initiAutoPageTracking() {
|
|
763
|
+
var _a, _b;
|
|
741
764
|
// Disable auto page tracking if the config is set to false
|
|
742
|
-
|
|
743
|
-
if (this.config.autoPageTracking === false) return;
|
|
765
|
+
if (!((_a = this.config.autoPageTracking) === null || _a === void 0 ? void 0 : _a.enabled)) return;
|
|
744
766
|
// Prevent running on the server
|
|
745
767
|
if (!isBrowser) return console.warn('Saasco auto page tracking is only available in the browser');
|
|
746
768
|
// Prevent intitializing auto page tracking more than once
|
|
@@ -752,8 +774,10 @@ class Saasco {
|
|
|
752
774
|
this.log('Auto Page Tracking enabled');
|
|
753
775
|
// Track initial page load
|
|
754
776
|
this.page();
|
|
755
|
-
// Listen for hash changes
|
|
756
|
-
|
|
777
|
+
// Listen for hash changes if hash tracking is enabled
|
|
778
|
+
if ((_b = this.config.autoPageTracking) === null || _b === void 0 ? void 0 : _b.trackHash) {
|
|
779
|
+
window.addEventListener('hashchange', () => this.page());
|
|
780
|
+
}
|
|
757
781
|
// Listen to popstate for back and forward navigation
|
|
758
782
|
window.addEventListener('popstate', () => this.page());
|
|
759
783
|
// Wrap history push state to listen for URL changes
|
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;
|
|
@@ -22,11 +26,13 @@ export declare class Saasco {
|
|
|
22
26
|
constructor(config: {
|
|
23
27
|
projectId: string;
|
|
24
28
|
proxy?: string;
|
|
25
|
-
autoPageTracking?:
|
|
29
|
+
autoPageTracking?: {
|
|
30
|
+
enabled: boolean;
|
|
31
|
+
trackQueryParams?: boolean;
|
|
32
|
+
trackHash?: boolean;
|
|
33
|
+
};
|
|
26
34
|
enabled?: boolean;
|
|
27
35
|
debug?: boolean;
|
|
28
|
-
trackUrlParams?: boolean;
|
|
29
|
-
trackHashChanges?: boolean;
|
|
30
36
|
});
|
|
31
37
|
init(): void;
|
|
32
38
|
disableDebug(): void;
|
|
@@ -42,14 +48,14 @@ export declare class Saasco {
|
|
|
42
48
|
*/
|
|
43
49
|
track(name: string, properties?: Record<string, any>, context?: {
|
|
44
50
|
active?: boolean;
|
|
45
|
-
}):
|
|
51
|
+
}): Promise<DoRequestResponse>;
|
|
46
52
|
/**
|
|
47
53
|
* The page method lets you record page views on your website
|
|
48
54
|
* This records the page title and path and names the event useing the reserved property "Page Viewed"
|
|
49
55
|
*
|
|
50
56
|
* Before implementing this make sure you have disabled the autoPageTracking in the config or you will get duplicate page views
|
|
51
57
|
*/
|
|
52
|
-
page():
|
|
58
|
+
page(): Promise<DoRequestResponse> | undefined;
|
|
53
59
|
/**
|
|
54
60
|
* The identify method lets you tie a user to their actions and record traits about them.
|
|
55
61
|
* We recommend you call this when the user logs in and when any traits get updated.
|
|
@@ -61,10 +67,10 @@ export declare class Saasco {
|
|
|
61
67
|
*/
|
|
62
68
|
identify(properties: Record<string, any>, context?: {
|
|
63
69
|
active?: boolean;
|
|
64
|
-
}):
|
|
70
|
+
}): Promise<DoRequestResponse>;
|
|
65
71
|
identify(distinctId: string | number | null, properties?: Record<string, any>, context?: {
|
|
66
72
|
active?: boolean;
|
|
67
|
-
}):
|
|
73
|
+
}): Promise<DoRequestResponse>;
|
|
68
74
|
/**
|
|
69
75
|
* Handles sending data to the Saasco API
|
|
70
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
|
|
@@ -87,3 +93,4 @@ export declare class Saasco {
|
|
|
87
93
|
*/
|
|
88
94
|
private initiAutoPageTracking;
|
|
89
95
|
}
|
|
96
|
+
export {};
|