saasco-sdk 0.1.10 → 0.1.12
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 +12 -9
- package/index.cjs.js +34 -4
- package/index.esm.js +34 -4
- package/package.json +1 -1
- package/src/lib/analytics.d.ts +8 -3
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
- [Privacy](#privacy)
|
|
6
6
|
- [Getting Started](#initiate-the-lib)
|
|
7
|
-
- [Automatic Page View Tracking](#automatic-page-view-tracking)
|
|
7
|
+
- [Init Automatic Page View Tracking](#init-automatic-page-view-tracking)
|
|
8
8
|
- [Tracking Events](#tracking-events)
|
|
9
9
|
- [Identifying Users](#identifying-users)
|
|
10
10
|
- [Debugging and Dev](#debugging-and-dev)
|
|
@@ -27,22 +27,25 @@ Copy your Project ID from the project settings page in Saasco:
|
|
|
27
27
|
Then import Saasco and initiate the lib with your Project ID.
|
|
28
28
|
|
|
29
29
|
```ts
|
|
30
|
-
|
|
31
30
|
// lib/saasco.ts
|
|
32
31
|
import { Saasco } from 'saasco-sdk';
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
const saasco = new Saasco({ projectId: "YOUR-PROJECT-ID" });
|
|
36
|
-
saasco.init();
|
|
37
|
-
|
|
38
|
-
export saasco;
|
|
33
|
+
export const saasco = new Saasco({ projectId: 'YOUR-PROJECT-ID' });
|
|
39
34
|
```
|
|
40
35
|
|
|
41
|
-
## Automatic Page View Tracking
|
|
36
|
+
## Init Automatic Page View Tracking
|
|
42
37
|
|
|
43
|
-
When you initial the lib
|
|
38
|
+
When you initial the lib Saasco will automatically start tracking all page views in your app. There is no other configuration. It will automatically track url changes, even for SPAs like NextJs and Vue.
|
|
44
39
|
You can also [manually track pages](https://www.notion.so/Manual-Page-Tracking-in-SPAs-2442fc7586dc4208ae8f669eb7561b1a?pvs=21) by opting our of automatic page tracking. For most use cases you don't need to do this.
|
|
45
40
|
|
|
41
|
+
```ts
|
|
42
|
+
// ./app/index
|
|
43
|
+
import saasco from './lib/saasco';
|
|
44
|
+
|
|
45
|
+
// This should be called once in your app and will start auto page tracking if you have it enabled.
|
|
46
|
+
saasco.init();
|
|
47
|
+
```
|
|
48
|
+
|
|
46
49
|
## Tracking Events
|
|
47
50
|
|
|
48
51
|
With events you can track custom actions users are taking on your site with event properties.
|
package/index.cjs.js
CHANGED
|
@@ -434,7 +434,7 @@ const timezones = {
|
|
|
434
434
|
'Asia/Calcutta': 'IN'
|
|
435
435
|
};
|
|
436
436
|
|
|
437
|
-
var version = "0.1.
|
|
437
|
+
var version = "0.1.12";
|
|
438
438
|
|
|
439
439
|
const isBrowser = typeof window !== 'undefined';
|
|
440
440
|
let userId = null;
|
|
@@ -547,21 +547,36 @@ class Saasco {
|
|
|
547
547
|
this.config = config;
|
|
548
548
|
this.lastPageViewHref = '';
|
|
549
549
|
this.isInitialized = false;
|
|
550
|
+
if (!config.projectId) {
|
|
551
|
+
this.error("Project ID is required but has not been provided. If you are using an env variable make sure it's set correctly.");
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
550
554
|
// default enabled to true
|
|
551
555
|
this.config.enabled = this.config.enabled === undefined ? true : this.config.enabled;
|
|
552
556
|
}
|
|
553
557
|
init() {
|
|
558
|
+
if (!this.config.projectId) {
|
|
559
|
+
return this.error("Unable to initialize Saasco. Project ID is required but has not been provided. If you are using an env variable make sure it's set correctly.");
|
|
560
|
+
}
|
|
554
561
|
if (this.isInitialized) {
|
|
555
562
|
this.log('Saasco is already initialized. Please check your code to ensure that init() is not being called multiple times.');
|
|
556
563
|
return;
|
|
557
564
|
}
|
|
565
|
+
// Export Saasco to the window object for easy access and debugging
|
|
566
|
+
if (isBrowser) {
|
|
567
|
+
window.saasco = this;
|
|
568
|
+
}
|
|
558
569
|
this.log('Saasco initialized', this.config);
|
|
559
570
|
this.initiAutoPageTracking();
|
|
560
571
|
this.isInitialized = true;
|
|
561
572
|
}
|
|
562
|
-
|
|
563
|
-
this.
|
|
564
|
-
this.
|
|
573
|
+
disableDebug() {
|
|
574
|
+
this.log('Debug mode deactivated.');
|
|
575
|
+
this.config.debug = false;
|
|
576
|
+
}
|
|
577
|
+
enableDebug() {
|
|
578
|
+
this.config.debug = true;
|
|
579
|
+
this.log('Debug mode activated.');
|
|
565
580
|
}
|
|
566
581
|
/**
|
|
567
582
|
* The track method lets you record the actions your users perform.
|
|
@@ -575,6 +590,9 @@ class Saasco {
|
|
|
575
590
|
track(name,
|
|
576
591
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
577
592
|
properties, context) {
|
|
593
|
+
if (!this.config.projectId) {
|
|
594
|
+
return 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.");
|
|
595
|
+
}
|
|
578
596
|
// Prevent duplicate Page View tracking
|
|
579
597
|
if (name === 'Page View') {
|
|
580
598
|
if (!isBrowser) {
|
|
@@ -614,6 +632,9 @@ class Saasco {
|
|
|
614
632
|
return this.track('Page View');
|
|
615
633
|
}
|
|
616
634
|
identify(distinctIdOrProperties, propertiesOrContext, contextOrNothing) {
|
|
635
|
+
if (!this.config.projectId) {
|
|
636
|
+
return 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.");
|
|
637
|
+
}
|
|
617
638
|
const hasId = typeof distinctIdOrProperties === 'string' || typeof distinctIdOrProperties === 'number' || distinctIdOrProperties === null;
|
|
618
639
|
const distinctId = hasId ? distinctIdOrProperties === null || distinctIdOrProperties === void 0 ? void 0 : distinctIdOrProperties.toString() : `soft_${uuid.v4()}`;
|
|
619
640
|
const properties = hasId ? propertiesOrContext : distinctIdOrProperties;
|
|
@@ -690,6 +711,15 @@ class Saasco {
|
|
|
690
711
|
// Message highlighted for easy finding
|
|
691
712
|
...args]);
|
|
692
713
|
}
|
|
714
|
+
/**
|
|
715
|
+
* @param args Arguments to be logged
|
|
716
|
+
*/
|
|
717
|
+
error(...args) {
|
|
718
|
+
const message = '◍ Saasco Error';
|
|
719
|
+
console.error(...[`\x1b[41m\x1b[37m ${message} \x1b[0m`,
|
|
720
|
+
// Message highlighted for easy finding
|
|
721
|
+
...args]);
|
|
722
|
+
}
|
|
693
723
|
/**
|
|
694
724
|
* If autoPageTracking is enabled, this will automatically track page views
|
|
695
725
|
* It listens to url changes to track new pages every time the url changes
|
package/index.esm.js
CHANGED
|
@@ -430,7 +430,7 @@ const timezones = {
|
|
|
430
430
|
'Asia/Calcutta': 'IN'
|
|
431
431
|
};
|
|
432
432
|
|
|
433
|
-
var version = "0.1.
|
|
433
|
+
var version = "0.1.12";
|
|
434
434
|
|
|
435
435
|
const isBrowser = typeof window !== 'undefined';
|
|
436
436
|
let userId = null;
|
|
@@ -543,21 +543,36 @@ class Saasco {
|
|
|
543
543
|
this.config = config;
|
|
544
544
|
this.lastPageViewHref = '';
|
|
545
545
|
this.isInitialized = false;
|
|
546
|
+
if (!config.projectId) {
|
|
547
|
+
this.error("Project ID is required but has not been provided. If you are using an env variable make sure it's set correctly.");
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
546
550
|
// default enabled to true
|
|
547
551
|
this.config.enabled = this.config.enabled === undefined ? true : this.config.enabled;
|
|
548
552
|
}
|
|
549
553
|
init() {
|
|
554
|
+
if (!this.config.projectId) {
|
|
555
|
+
return this.error("Unable to initialize Saasco. Project ID is required but has not been provided. If you are using an env variable make sure it's set correctly.");
|
|
556
|
+
}
|
|
550
557
|
if (this.isInitialized) {
|
|
551
558
|
this.log('Saasco is already initialized. Please check your code to ensure that init() is not being called multiple times.');
|
|
552
559
|
return;
|
|
553
560
|
}
|
|
561
|
+
// Export Saasco to the window object for easy access and debugging
|
|
562
|
+
if (isBrowser) {
|
|
563
|
+
window.saasco = this;
|
|
564
|
+
}
|
|
554
565
|
this.log('Saasco initialized', this.config);
|
|
555
566
|
this.initiAutoPageTracking();
|
|
556
567
|
this.isInitialized = true;
|
|
557
568
|
}
|
|
558
|
-
|
|
559
|
-
this.
|
|
560
|
-
this.
|
|
569
|
+
disableDebug() {
|
|
570
|
+
this.log('Debug mode deactivated.');
|
|
571
|
+
this.config.debug = false;
|
|
572
|
+
}
|
|
573
|
+
enableDebug() {
|
|
574
|
+
this.config.debug = true;
|
|
575
|
+
this.log('Debug mode activated.');
|
|
561
576
|
}
|
|
562
577
|
/**
|
|
563
578
|
* The track method lets you record the actions your users perform.
|
|
@@ -571,6 +586,9 @@ class Saasco {
|
|
|
571
586
|
track(name,
|
|
572
587
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
573
588
|
properties, context) {
|
|
589
|
+
if (!this.config.projectId) {
|
|
590
|
+
return 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.");
|
|
591
|
+
}
|
|
574
592
|
// Prevent duplicate Page View tracking
|
|
575
593
|
if (name === 'Page View') {
|
|
576
594
|
if (!isBrowser) {
|
|
@@ -610,6 +628,9 @@ class Saasco {
|
|
|
610
628
|
return this.track('Page View');
|
|
611
629
|
}
|
|
612
630
|
identify(distinctIdOrProperties, propertiesOrContext, contextOrNothing) {
|
|
631
|
+
if (!this.config.projectId) {
|
|
632
|
+
return 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.");
|
|
633
|
+
}
|
|
613
634
|
const hasId = typeof distinctIdOrProperties === 'string' || typeof distinctIdOrProperties === 'number' || distinctIdOrProperties === null;
|
|
614
635
|
const distinctId = hasId ? distinctIdOrProperties === null || distinctIdOrProperties === void 0 ? void 0 : distinctIdOrProperties.toString() : `soft_${v4()}`;
|
|
615
636
|
const properties = hasId ? propertiesOrContext : distinctIdOrProperties;
|
|
@@ -686,6 +707,15 @@ class Saasco {
|
|
|
686
707
|
// Message highlighted for easy finding
|
|
687
708
|
...args]);
|
|
688
709
|
}
|
|
710
|
+
/**
|
|
711
|
+
* @param args Arguments to be logged
|
|
712
|
+
*/
|
|
713
|
+
error(...args) {
|
|
714
|
+
const message = '◍ Saasco Error';
|
|
715
|
+
console.error(...[`\x1b[41m\x1b[37m ${message} \x1b[0m`,
|
|
716
|
+
// Message highlighted for easy finding
|
|
717
|
+
...args]);
|
|
718
|
+
}
|
|
689
719
|
/**
|
|
690
720
|
* If autoPageTracking is enabled, this will automatically track page views
|
|
691
721
|
* It listens to url changes to track new pages every time the url changes
|
package/package.json
CHANGED
package/src/lib/analytics.d.ts
CHANGED
|
@@ -24,7 +24,8 @@ export declare class Saasco {
|
|
|
24
24
|
debug?: boolean;
|
|
25
25
|
});
|
|
26
26
|
init(): void;
|
|
27
|
-
|
|
27
|
+
disableDebug(): void;
|
|
28
|
+
enableDebug(): void;
|
|
28
29
|
/**
|
|
29
30
|
* The track method lets you record the actions your users perform.
|
|
30
31
|
* Its good to keep a conistent naming convention for your events.
|
|
@@ -36,14 +37,14 @@ export declare class Saasco {
|
|
|
36
37
|
*/
|
|
37
38
|
track(name: string, properties?: Record<string, any>, context?: {
|
|
38
39
|
active?: boolean;
|
|
39
|
-
}): Promise<string | Response | undefined
|
|
40
|
+
}): void | Promise<string | Response | undefined>;
|
|
40
41
|
/**
|
|
41
42
|
* The page method lets you record page views on your website
|
|
42
43
|
* This records the page title and path and names the event useing the reserved property "Page Viewed"
|
|
43
44
|
*
|
|
44
45
|
* Before implementing this make sure you have disabled the autoPageTracking in the config or you will get duplicate page views
|
|
45
46
|
*/
|
|
46
|
-
page(): Promise<string | Response | undefined
|
|
47
|
+
page(): void | Promise<string | Response | undefined>;
|
|
47
48
|
/**
|
|
48
49
|
* The identify method lets you tie a user to their actions and record traits about them.
|
|
49
50
|
* We recommend you call this when the user logs in and when any traits get updated.
|
|
@@ -70,6 +71,10 @@ export declare class Saasco {
|
|
|
70
71
|
* @param args Arguments to be logged
|
|
71
72
|
*/
|
|
72
73
|
private log;
|
|
74
|
+
/**
|
|
75
|
+
* @param args Arguments to be logged
|
|
76
|
+
*/
|
|
77
|
+
private error;
|
|
73
78
|
/**
|
|
74
79
|
* If autoPageTracking is enabled, this will automatically track page views
|
|
75
80
|
* It listens to url changes to track new pages every time the url changes
|