saasco-sdk 0.1.0
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 +504 -0
- package/package.json +13 -0
- package/src/index.d.ts +3 -0
- package/src/index.js +4 -0
- package/src/index.js.map +1 -0
- package/src/lib/analytics.d.ts +77 -0
- package/src/lib/analytics.js +277 -0
- package/src/lib/analytics.js.map +1 -0
- package/src/lib/coerceReservedProperties.d.ts +2 -0
- package/src/lib/coerceReservedProperties.js +305 -0
- package/src/lib/coerceReservedProperties.js.map +1 -0
- package/src/lib/self-execute-analytics.d.ts +6 -0
- package/src/lib/self-execute-analytics.js +38 -0
- package/src/lib/self-execute-analytics.js.map +1 -0
- package/src/lib/timezones.d.ts +3 -0
- package/src/lib/timezones.js +428 -0
- package/src/lib/timezones.js.map +1 -0
- package/src/lib/types.d.ts +1303 -0
- package/src/lib/types.js +177 -0
- package/src/lib/types.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
[Saasco](https://www.saasco.com) is a platform of saas apps where members only pay the infrastructure cost of what they use. As a result Saasco apps are up to 99% cheaper than traditional saas products.
|
|
2
|
+
|
|
3
|
+
The analytics package makes it easy for javascript projects to track website activity.
|
|
4
|
+
Analytics is a core feature of Saasco and is used to power many other apps.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
- [Privacy](#privacy)
|
|
9
|
+
- [Getting Started](#initiate-the-lib)
|
|
10
|
+
- [Naming Conflict](#naming-conflict)
|
|
11
|
+
- [Automatic Page View Tracking](#automatic-page-view-tracking)
|
|
12
|
+
- [Tracking Events](#tracking-events)
|
|
13
|
+
- [Identifying Users](#identifying-users)
|
|
14
|
+
- [Debugging and Dev](#debugging-and-dev)
|
|
15
|
+
- [Reserved Properties](#reserved-properties)
|
|
16
|
+
|
|
17
|
+
<br />
|
|
18
|
+
|
|
19
|
+
## 🔒 Privacy
|
|
20
|
+
|
|
21
|
+
By default Saasco is privacy friendly, using no cookies and obscuring all activity behind Anonymous IDs. However if you want to power more complex user tracking you can identify users and then all events they do will be attributed to them in your CRM. This can assist with customer support but also for things like drip campaigns and other marketing activities.
|
|
22
|
+
|
|
23
|
+
## Getting Started:
|
|
24
|
+
|
|
25
|
+
Install by running:
|
|
26
|
+
`npm install saasco-analytics`
|
|
27
|
+
|
|
28
|
+
Copy your Project ID from the project settings page in Saasco:
|
|
29
|
+
[https://saasco.com/your-project/settings/project](https://saasco.com/your-projects/settings/project)
|
|
30
|
+
|
|
31
|
+
Then import Analytics and initiate the lib with your Project ID.
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { Analytics } from 'saasco-analytics';
|
|
35
|
+
|
|
36
|
+
// Initiate the lib
|
|
37
|
+
export const analytics = new Analytics({ projectId: "YOUR-PROJECT-ID" });
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Naming Conflict
|
|
41
|
+
|
|
42
|
+
Because “Analytics” is a common name for analytics tools you can simply import with a different name such as “SaascoAnalytics” to avoid conflicts.
|
|
43
|
+
|
|
44
|
+
For example:
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
import { Analytics as SaascoAnalytics } from 'saasco-analytics';
|
|
48
|
+
|
|
49
|
+
// Initiate the lib
|
|
50
|
+
const saascoAnalytics = new SaascoAnalytics({ projectId: "YOUR-PROJECT-ID" });
|
|
51
|
+
|
|
52
|
+
// Track an event
|
|
53
|
+
saascoAnalytics.track('Something happened');
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Automatic Page View Tracking
|
|
57
|
+
|
|
58
|
+
When you initial the lib live above 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.
|
|
59
|
+
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.
|
|
60
|
+
|
|
61
|
+
## Tracking Events
|
|
62
|
+
|
|
63
|
+
With events you can track custom actions users are taking on your site with event properties.
|
|
64
|
+
|
|
65
|
+
Each event has 2 components:
|
|
66
|
+
|
|
67
|
+
**Name** - this is the name of the action that was taken.
|
|
68
|
+
|
|
69
|
+
_We like to follow segments [Object Action Framework](https://segment.com/academy/collecting-data/naming-conventions-for-clean-data/) for naming events._
|
|
70
|
+
|
|
71
|
+
**Properties** - This is an object with the values of the event. Such as a the value, currency, or query.
|
|
72
|
+
|
|
73
|
+
We currently have limited support for properties, but in the future we will support complex querying and actions based on event properties.
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
// Import analytics
|
|
77
|
+
import { analytics } from '../your-analytics';
|
|
78
|
+
|
|
79
|
+
// Track an event
|
|
80
|
+
analytics.track("Searched Movies");
|
|
81
|
+
|
|
82
|
+
// Track an event with properties
|
|
83
|
+
analytics.track("Searched Movies", {
|
|
84
|
+
query:"batman",
|
|
85
|
+
sortBy:"relevancy"
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Identifying Users
|
|
90
|
+
|
|
91
|
+
By default Saasco does not track any identifiable data about users, just anonymous user ids and session ids. However if you want to tie events to users you can identify users which will connect events to users in your CRM and power tools like drip campaigns and customer support.
|
|
92
|
+
|
|
93
|
+
To do this just identify a user with their DistinctId (usually the user ID from your database) and then any properties on the user.
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
// Import analytics
|
|
97
|
+
import { analytics } from '../your-analytics';
|
|
98
|
+
|
|
99
|
+
// Inside your user logged in function identify a user
|
|
100
|
+
// You should only identify users when they login, or their properties change
|
|
101
|
+
function signedIn(){
|
|
102
|
+
// Identify a user
|
|
103
|
+
analytics.identify("USER-ID-FROM-YOUR-DB", {
|
|
104
|
+
// Any user properties you want to record with the user
|
|
105
|
+
name:"Tony Hawk",
|
|
106
|
+
email:"tony@xgames.com",
|
|
107
|
+
bestTrick: 900
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// When a user logs out you can unidentify by identify null
|
|
112
|
+
function signedOut(){
|
|
113
|
+
analtyics.identify(null)
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Identifying users who don't have an ID
|
|
118
|
+
|
|
119
|
+
Often times you have a users contact details before they sign up for your site. This could be part of the signup flow, or if they were to subscribe to marketing emails before they sign up as a customer
|
|
120
|
+
|
|
121
|
+
Saasco allows identifying users without an id by simply skipping the `ID` part of the identify call. This will add contacts to the CRM for email marketing and other tools before users register.
|
|
122
|
+
|
|
123
|
+
For example:
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
// Identify a user only by their email
|
|
127
|
+
analytics.identify({
|
|
128
|
+
name:"Tony Hawk",
|
|
129
|
+
email:"tony@xgames.com",
|
|
130
|
+
bestTrick: 900
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
By doing this users will get tracked and added to your contacts, and as soon as they do login, assuming they use the same email address, all their properties will be connected.
|
|
135
|
+
|
|
136
|
+
### Soft vs Full identify
|
|
137
|
+
|
|
138
|
+
When users are identified without a `ID` we call this is a "Soft Identify". Users who are soft identified can also be called "Leads".
|
|
139
|
+
Once a user signs up and you identify them with an `ID` they will be fully registered and can be called "Customers"
|
|
140
|
+
|
|
141
|
+
In the CRM when you identify a user, either soft or full, they become a contact.
|
|
142
|
+
The type of the contact depends on whether they were fully identified, "Leads" are soft and "Customers" are full.
|
|
143
|
+
|
|
144
|
+
## Debugging and Dev
|
|
145
|
+
|
|
146
|
+
When setting up Saasco analytics you probably want to exclude your local environment.
|
|
147
|
+
|
|
148
|
+
To do so simply turn on dev mode.
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
export const analytics = new Analytics({
|
|
152
|
+
projectId: 'YOUR-PROJECT-ID',
|
|
153
|
+
|
|
154
|
+
// use an env to only set enabled to true on production
|
|
155
|
+
enabled: process.env['NODE_ENV'] === 'production',
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### Debugging
|
|
160
|
+
|
|
161
|
+
You can turn on debugging when you initiate the repo or by running analytics.debug(true) in your code or the browser.
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
export const analytics = new Analytics({
|
|
165
|
+
projectId: 'YOUR-PROJECT-ID',
|
|
166
|
+
enabled: process.env['NODE_ENV'] === 'production',
|
|
167
|
+
// Set to true to console log analytics events.
|
|
168
|
+
// This works even when enabled is false - it just wont send the data
|
|
169
|
+
debug: true,
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
You can also enable debug mode by calling `analytics.debug(true)` anywhere in your code.
|
|
174
|
+
|
|
175
|
+
```tsx
|
|
176
|
+
analytics.debug(true);
|
|
177
|
+
// will console log "debug mode activated"
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Reserved Properties
|
|
181
|
+
|
|
182
|
+
Saasco has reserved some properties that have semantic meanings for contacts and, and will handle them in special ways.
|
|
183
|
+
For example, Saasco always expects email to be a string of the user’s email address, this is important for when sending emails using the marketing app.
|
|
184
|
+
|
|
185
|
+
### Reserved Contact Properties
|
|
186
|
+
|
|
187
|
+
| PROPTERTY | TYPE | DESCRIPTION |
|
|
188
|
+
| ----------- | ------ | ----------------------------------------------------------------------------------------------------------------- |
|
|
189
|
+
| age | Number | Age of a user |
|
|
190
|
+
| avatar | String | URL to an avatar image for the user |
|
|
191
|
+
| birthday | Date | User’s birthday |
|
|
192
|
+
| createdAt | Date | Date the user’s account was first created. We recomend recommend using ISO-8601 date strings. |
|
|
193
|
+
| description | String | Description of the user |
|
|
194
|
+
| email | String | Email address of a user |
|
|
195
|
+
| firstName | String | First name of a user |
|
|
196
|
+
| displayName | String | The prefered users display name, will default to "firstName lastName" |
|
|
197
|
+
| gender | String | Gender of a user |
|
|
198
|
+
| id | String | The ID of the user from your database, coerced to a string. eg 1 becomes "1" |
|
|
199
|
+
| lastName | String | Last name of a user |
|
|
200
|
+
| name | String | Full name of a user. If you only pass a first and last name Segment automatically fills in the full name for you. |
|
|
201
|
+
| phone | String | Phone number of a user |
|
|
202
|
+
| title | String | Title of a user, usually related to their position at a specific company. Example: “VP of Engineering” |
|
|
203
|
+
| username | String | User’s username. This should be unique to each user, like the usernames of Twitter or GitHub. |
|
|
204
|
+
| website | String | Website of a user |
|
|
205
|
+
|
|
206
|
+
### Default Contact Properties
|
|
207
|
+
|
|
208
|
+
Our SDKs automatically collect certain properties on every event or user profile.
|
|
209
|
+
Any property that starts with `$` is a property that has been generated by the SDK or ingestion.
|
|
210
|
+
|
|
211
|
+
| Property | Display Name | Description |
|
|
212
|
+
| ----------------------- | --------------------------- | -------------------------------------------------------------------------------------- |
|
|
213
|
+
| $city | City | The city of the user parsed from the IP. |
|
|
214
|
+
| $region | Region | The region of the user parsed from the IP |
|
|
215
|
+
| $countryCode | Country Code | The country of the user parsed from the IP property. |
|
|
216
|
+
| $latitude | Latitude | Latitude of the user's IP location. |
|
|
217
|
+
| $longitude | Longitude | Longitude of the user's IP location. |
|
|
218
|
+
| $timezone | Timezone | Timezone of the user parsed from the IP. |
|
|
219
|
+
| $screenHeight | Screen Height | The most recent height of the device screen in pixels |
|
|
220
|
+
| $screenWidth | Screen Width | The most recent width of the device screen in pixels |
|
|
221
|
+
| $screenDpi | Screen DPI | The most recent Pixel density of the device screen. |
|
|
222
|
+
| $lastSeen | Updated at | The last time a user profile property was set or updated |
|
|
223
|
+
| $os | Operating System | The most recent OS of the user. |
|
|
224
|
+
| $browser | Browser | The most recent browser of the user. |
|
|
225
|
+
| $browserVersion | Browser Version | The most recent bowser version of the user. |
|
|
226
|
+
| $initialReferrer | Initial Referrer | Referring URL when the user first arrived on your site. Defaults to "direct" |
|
|
227
|
+
| $referrer | Last Touch Referrer | Referring URL when the user last interacted with your site. Defaults to "direct" |
|
|
228
|
+
| $initialReferringDomain | Initial Referring Domain | Referring domain at first arrival. Defaults to "direct" |
|
|
229
|
+
| $referringDomain | Last Touch Referring Domain | Referring domain at the user's last interaction. Defaults to "direct" |
|
|
230
|
+
| $initialUtmSource | Initial UTM Source | The initial UTM source tag from the URL a customer clicked to arrive at your domain. |
|
|
231
|
+
| $utmSource | Last Touch UTM Source | The UTM source tag from the URL a customer clicked during their last interaction. |
|
|
232
|
+
| $initialUtmMedium | Initial UTM Medium | The initial UTM medium tag from the URL a customer clicked to arrive at your domain. |
|
|
233
|
+
| $utmMedium | Last Touch UTM Medium | The UTM medium tag from the URL a customer clicked during their last interaction. |
|
|
234
|
+
| $initialUtmCampaign | Initial UTM Campaign | The initial UTM campaign tag from the URL a customer clicked to arrive at your domain. |
|
|
235
|
+
| $utmCampaign | Last Touch UTM Campaign | The UTM campaign tag from the URL a customer clicked during their last interaction. |
|
|
236
|
+
| $initialUtmTerm | Initial UTM Term | The initial UTM term tag from the URL a customer clicked to arrive at your domain. |
|
|
237
|
+
| $utmTerm | Last Touch UTM Term | The UTM term tag from the URL a customer clicked during their last interaction. |
|
|
238
|
+
| $initialUtmContent | Initial UTM Content | The initial UTM content tag from the URL a customer clicked to arrive at your domain. |
|
|
239
|
+
| $utmContent | Last Touch UTM Content | The UTM content tag from the URL a customer clicked during their last interaction. |
|
|
240
|
+
| $unsubscribed | Unsubscribed | Whether the user has unsubscribed from all notifications |
|
|
241
|
+
|
|
242
|
+
### Reserved event properties
|
|
243
|
+
|
|
244
|
+
Properties used to calculate revenue for different traffic sources and LTV for users.
|
|
245
|
+
|
|
246
|
+
| PROPERTY | TYPE | DESCRIPTION |
|
|
247
|
+
| -------- | ------ | ----------------------------------------------------------------------------------------- |
|
|
248
|
+
| revenue | Number | Amount of revenue an event resulted in. This should be a decimal value |
|
|
249
|
+
| currency | String | Currency of the revenue an event resulted in. This should be sent in the ISO 4127 format. |
|
|
250
|
+
| value | Number | An abstract numerical value used internally to score eventsm such as lead scoring. |
|
|
251
|
+
|
|
252
|
+
### Default Event Properties
|
|
253
|
+
|
|
254
|
+
Our SDKs automatically collect certain properties on every event or user profile. The default properties begin with a `$` and can be overwritten with your own identify calls, so we recomend avoiding leading `$` in your identify call properties to avoid conflicts.
|
|
255
|
+
|
|
256
|
+
| Property | Display Name | Description |
|
|
257
|
+
| ---------------- | --------------------------- | -------------------------------------------------------------------------------- |
|
|
258
|
+
| $city | City | The city of the user parsed from the IP. |
|
|
259
|
+
| $region | Region | The region of the user parsed from the IP |
|
|
260
|
+
| $countryCode | Country Code | The country of the user parsed from the IP property. |
|
|
261
|
+
| $latitude | Latitude | Latitude of the user's IP location. |
|
|
262
|
+
| $longitude | Longitude | Longitude of the user's IP location. |
|
|
263
|
+
| $timezone | Timezone | Timezone of the user parsed from the IP. |
|
|
264
|
+
| $screenHeight | Screen Height | The height of the device screen in pixels |
|
|
265
|
+
| $screenWidth | Screen Width | The width of the device screen in pixels |
|
|
266
|
+
| $screenDpi | Screen DPI | Pixel density of the device screen. |
|
|
267
|
+
| $currentUrl | Current URL | The URL of the page on which the event was tracked. |
|
|
268
|
+
| $os | Operating System | The most recent OS of the user. |
|
|
269
|
+
| $browser | Browser | The most recent browser of the user. |
|
|
270
|
+
| $browserVersion | Browser Version | The most recent bowser version of the user. |
|
|
271
|
+
| $device | Device Type | The most recent bowser version of the user. eg `Mobile`, `Tablet`, `Desktop` |
|
|
272
|
+
| $referrer | Last Touch Referrer | Referring URL when the user last interacted with your site. Defaults to "direct" |
|
|
273
|
+
| $referringDomain | Last Touch Referring Domain | Referring domain at the user's last interaction. Defaults to "direct" |
|
|
274
|
+
| $utmSource | UTM Source | The UTM source tag from the URL a customer clicked to arrive at your domain. |
|
|
275
|
+
| $utmMedium | UTM Medium | The UTM medium tag from the URL a customer clicked to arrive at your domain. |
|
|
276
|
+
| $utmCampaign | UTM Campaign | The UTM campaign tag from the URL a customer clicked to arrive at your domain. |
|
|
277
|
+
| $utmTerm | UTM Term | The UTM term tag from the URL a customer clicked to arrive at your domain. |
|
|
278
|
+
| $utmContent | UTM Content | The UTM content tag from the URL a customer clicked to arrive at your domain. |
|
|
279
|
+
|
|
280
|
+
### Reserved Events with Optional Properties
|
|
281
|
+
|
|
282
|
+
> ⚠️ The event schema described below is not yet implemented in the UI. However, if you wish to future-proof your implementation, you can start using this schema in anticipation of its integration.
|
|
283
|
+
|
|
284
|
+
Reserved events are common events that happen during the lifecycle of a user, with optional properties to capture more detailed information, including revenue data where appropriate.
|
|
285
|
+
|
|
286
|
+
- [Signed Up](#signed-up)
|
|
287
|
+
- [Signed In](#signed-in)
|
|
288
|
+
- [Signed Out](#signed-out)
|
|
289
|
+
- [Trial Started](#trial-started)
|
|
290
|
+
- [Trial Completed](#trial-completed)
|
|
291
|
+
- [Payment Completed](#payment-completed)
|
|
292
|
+
- [Subscription Started](#subscription-started)
|
|
293
|
+
- [Subscription Cancelled](#subscription-cancelled)
|
|
294
|
+
- [Subscription Upgraded](#subscription-upgraded)
|
|
295
|
+
- [Subscription Downgraded](#subscription-downgraded)
|
|
296
|
+
|
|
297
|
+
#### Signed Up
|
|
298
|
+
|
|
299
|
+
Event triggered when a user signs up.
|
|
300
|
+
| Property | Description |
|
|
301
|
+
|----------|----------------------------------|
|
|
302
|
+
| `source` | How the user found your site. |
|
|
303
|
+
| `value` | Track an estimated value of the signup|
|
|
304
|
+
|
|
305
|
+
Example:
|
|
306
|
+
|
|
307
|
+
```jsx
|
|
308
|
+
analytics.track('Signed Up', {
|
|
309
|
+
source: 'Referral by friend',
|
|
310
|
+
});
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
#### Signed In
|
|
314
|
+
|
|
315
|
+
Event triggered when a user signs in.
|
|
316
|
+
| Property | Description |
|
|
317
|
+
|----------|--------------------------------------------------|
|
|
318
|
+
| `method` | The method used for signing in (e.g., email, social media). |
|
|
319
|
+
|
|
320
|
+
Example:
|
|
321
|
+
|
|
322
|
+
```jsx
|
|
323
|
+
analytics.track('Signed In', {
|
|
324
|
+
method: 'email',
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
#### Signed Out
|
|
329
|
+
|
|
330
|
+
Event triggered when a user signs out.
|
|
331
|
+
No optional properties.
|
|
332
|
+
|
|
333
|
+
Example:
|
|
334
|
+
|
|
335
|
+
```jsx
|
|
336
|
+
analytics.track('Signed Out');
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
#### Trial Started
|
|
340
|
+
|
|
341
|
+
Event triggered when a user starts a trial.
|
|
342
|
+
| Property | Description |
|
|
343
|
+
|-------------------|---------------------------------------------------------------------|
|
|
344
|
+
| `duration` | The duration of the trial in days |
|
|
345
|
+
| `type`| Values can be `optIn` for then the user didn't provide a cc or `optOut` when the user provided a cc and it will automatically start and the end of the trial period|
|
|
346
|
+
|
|
347
|
+
Example:
|
|
348
|
+
|
|
349
|
+
```jsx
|
|
350
|
+
analytics.track('Trial Started', { duration: 14, type: 'optOut' });
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
#### Trial Completed
|
|
354
|
+
|
|
355
|
+
Event triggered when a user successfully completes a trial.
|
|
356
|
+
| Property | Description |
|
|
357
|
+
|-------------------|---------------------------------------------------------------------|
|
|
358
|
+
| `daysLeftInTrial`| If a user manual upgrades before the end of the trial you can record this here|
|
|
359
|
+
|
|
360
|
+
Example:
|
|
361
|
+
|
|
362
|
+
```jsx
|
|
363
|
+
analytics.track('Trial Completed', { daysLeftInTrial: 4 });
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
#### Payment Completed
|
|
367
|
+
|
|
368
|
+
Event triggered when a payment is completed.
|
|
369
|
+
This is may be called on the server side after a confirmation webhook.
|
|
370
|
+
This is helpful for recording ongoing subscription payments.
|
|
371
|
+
|
|
372
|
+
| Property | Description |
|
|
373
|
+
| ---------- | --------------------------------------------------- |
|
|
374
|
+
| `revenue` | The initial payment amount. |
|
|
375
|
+
| `currency` | The currency of the payment, otherwise assumed USD. |
|
|
376
|
+
|
|
377
|
+
Example:
|
|
378
|
+
|
|
379
|
+
```jsx
|
|
380
|
+
analytics.track('Payment Completed', { revenue: 29.99, currency: 'GBP' });
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
#### Subscription Started
|
|
384
|
+
|
|
385
|
+
Event triggered when a user starts a subscription.
|
|
386
|
+
If you have integrated with stripe for revenue tracking or are doing server side revenue tracking with webhooks you should skip the `revenue` and `currency` properties to prevent duplicate revenue tracking.
|
|
387
|
+
|
|
388
|
+
| Property | Description |
|
|
389
|
+
| ---------- | ----------------------------------------------------------------------------------------------------------------------- |
|
|
390
|
+
| `plan` | The name or ID of the subscription plan. |
|
|
391
|
+
| `revenue` | The initial payment amount. Exclude if triggering `Payment Completed` to avoid double counting. |
|
|
392
|
+
| `currency` | The currency of the payment, otherwise assumed USD. Exclude if triggering `Payment Completed` to avoid double counting. |
|
|
393
|
+
|
|
394
|
+
Example:
|
|
395
|
+
|
|
396
|
+
```jsx
|
|
397
|
+
analytics.track('Subscription Started', {
|
|
398
|
+
plan: 'Monthly',
|
|
399
|
+
});
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
#### Subscription Cancelled
|
|
403
|
+
|
|
404
|
+
Event triggered when a subscription is cancelled.
|
|
405
|
+
|
|
406
|
+
| Property | Description |
|
|
407
|
+
| -------- | -------------------------------------------------------------------------------------------------- |
|
|
408
|
+
| `reason` | The reason for cancellation. This can be used in the feedback and analysis of cancellation reasons |
|
|
409
|
+
|
|
410
|
+
Example:
|
|
411
|
+
|
|
412
|
+
```jsx
|
|
413
|
+
analytics.track('Subscription Cancelled', {
|
|
414
|
+
reason: 'Not using it any more',
|
|
415
|
+
});
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
#### Subscription Upgraded
|
|
419
|
+
|
|
420
|
+
Event triggered when a subscription is upgraded.
|
|
421
|
+
| Property | Description |
|
|
422
|
+
|-----------------|-----------------------------------------------------------------------------------------------|
|
|
423
|
+
| `fromPlan` | The previous plan. |
|
|
424
|
+
| `toPlan` | The new plan. |
|
|
425
|
+
| `previousRevenue` | The revenue from the previous plan. |
|
|
426
|
+
| `newRevenue` | The revenue from the new plan. |
|
|
427
|
+
| `revenue` | If the customer is charged immediately then include a revenue number here |
|
|
428
|
+
| `currency` | The currency of the payment, assumed to be USD unless otherwise specified. |
|
|
429
|
+
|
|
430
|
+
Example:
|
|
431
|
+
|
|
432
|
+
```jsx
|
|
433
|
+
analytics.track('Subscription Cancelled', {
|
|
434
|
+
fromPlan: 'Monthly',
|
|
435
|
+
toPlan: 'Annual',
|
|
436
|
+
previousRevenue: 29,
|
|
437
|
+
newRevenue: 129,
|
|
438
|
+
});
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
#### Subscription Downgraded
|
|
442
|
+
|
|
443
|
+
Event triggered when a subscription is downgraded.
|
|
444
|
+
| Property | Description |
|
|
445
|
+
|------------|-------------------|
|
|
446
|
+
| `fromPlan` | The previous plan. |
|
|
447
|
+
| `toPlan` | The new plan. |
|
|
448
|
+
| `previousRevenue` | The revenue from the previous plan. |
|
|
449
|
+
| `newRevenue` | The revenue from the new plan. |
|
|
450
|
+
| `revenue` | If the customer is refunded immediately then include a revenue number here, in the case of a downgrade this will be a negtive value. Only include the revenue here if it's not tracked on your backend or with a webhook |
|
|
451
|
+
| `currency` | The currency of the payment, assumed to be USD unless otherwise specified. |
|
|
452
|
+
|
|
453
|
+
Example:
|
|
454
|
+
|
|
455
|
+
```jsx
|
|
456
|
+
analytics.track('Subscription Cancelled', {
|
|
457
|
+
fromPlan: 'Annual',
|
|
458
|
+
toPlan: 'Monthly',
|
|
459
|
+
previousRevenue: 129,
|
|
460
|
+
newRevenue: 29,
|
|
461
|
+
revenue: -36,
|
|
462
|
+
currency: 'USD',
|
|
463
|
+
});
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
## Use the hosted SDK
|
|
467
|
+
|
|
468
|
+
To use the hosted JS SDK, add the following script to your HTML header:
|
|
469
|
+
All properties should be added as data attributes in the format data-[prop name]
|
|
470
|
+
|
|
471
|
+
```jsx
|
|
472
|
+
<script
|
|
473
|
+
src="https://saasco.com/sdk/analytics-sdk.js"
|
|
474
|
+
data-projectId="YOUR-PROJECT-ID"
|
|
475
|
+
></script>
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
All the properties for initiating the sdk can be passed in like this:
|
|
479
|
+
|
|
480
|
+
```jsx
|
|
481
|
+
<script
|
|
482
|
+
src="https://saasco.com/sdk/analytics-sdk.js"
|
|
483
|
+
data-projectId="YOUR-PROJECT-ID"
|
|
484
|
+
data-debug="true" // pass any properties as data params
|
|
485
|
+
></script>
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
You can then call the track and identify methods like so:
|
|
489
|
+
|
|
490
|
+
```jsx
|
|
491
|
+
analytics.track('Searched Movies');
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
For type safety in your TypeScript project, you can use the following in an `index.d.ts` file
|
|
495
|
+
|
|
496
|
+
```jsx
|
|
497
|
+
import { Analytics } from 'saasco-analytics';
|
|
498
|
+
|
|
499
|
+
declare global {
|
|
500
|
+
interface Window {
|
|
501
|
+
analytics?: Analytics;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "saasco-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"tslib": "^2.3.0",
|
|
6
|
+
"@lukeed/uuid": "^2.0.1",
|
|
7
|
+
"zod": "^3.22.4"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./src/index.js",
|
|
11
|
+
"typings": "./src/index.d.ts",
|
|
12
|
+
"module": "./src/index.js"
|
|
13
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../libs/analytics/shared/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Window {
|
|
3
|
+
saascoAutoPageTrackingActive?: boolean;
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export declare class Analytics {
|
|
7
|
+
private config;
|
|
8
|
+
private lastPageViewHref;
|
|
9
|
+
/**
|
|
10
|
+
* Creates an instance of Analytics.
|
|
11
|
+
* @param config Configuration options for analytics.
|
|
12
|
+
* @param config.projectId The unique identifier for the project.
|
|
13
|
+
* @param config.proxy The URL of the proxy server to use, if any.
|
|
14
|
+
* @param config.autoPageTracking Whether to automatically track page views. Default is false.
|
|
15
|
+
* @param config.enabled Whether analytics is enabled. Default is true. Set to false for development and staging envioronments. Will still allow debug mode to be true, just no events will be sent
|
|
16
|
+
* @param config.debug Whether to log debug information. Default is false.
|
|
17
|
+
*/
|
|
18
|
+
constructor(config: {
|
|
19
|
+
projectId: string;
|
|
20
|
+
proxy?: string;
|
|
21
|
+
autoPageTracking?: boolean;
|
|
22
|
+
enabled?: boolean;
|
|
23
|
+
debug?: boolean;
|
|
24
|
+
});
|
|
25
|
+
init(): void;
|
|
26
|
+
debug(value: boolean): void;
|
|
27
|
+
/**
|
|
28
|
+
* The track method lets you record the actions your users perform.
|
|
29
|
+
* Its good to keep a conistent naming convention for your events.
|
|
30
|
+
* We like the Object Action Framework from segment:
|
|
31
|
+
* https://segment.com/academy/collecting-data/naming-conventions-for-clean-data/
|
|
32
|
+
*
|
|
33
|
+
* @param name The name of the event eg "Song Played"
|
|
34
|
+
* @param properties The properties of the event eg { genre: "Classics", song: "Never Gonna Give You Up" }
|
|
35
|
+
*/
|
|
36
|
+
track(name: string, properties?: Record<string, any>): void;
|
|
37
|
+
/**
|
|
38
|
+
* The page method lets you record page views on your website
|
|
39
|
+
* This records the page title and path and names the event useing the reserved property "Page Viewed"
|
|
40
|
+
*
|
|
41
|
+
* Before implementing this make sure you have disabled the autoPageTracking in the config or you will get duplicate page views
|
|
42
|
+
*/
|
|
43
|
+
page(): void;
|
|
44
|
+
/**
|
|
45
|
+
* The identify method lets you tie a user to their actions and record traits about them.
|
|
46
|
+
* We recommend you call this when the user logs in and when any traits get updated.
|
|
47
|
+
* You can also identify a user as null when they logout to clear the user
|
|
48
|
+
*
|
|
49
|
+
* @param distinctId The user's id. This should be the user id from your database. This is optional and can be skipped, but you must provide an email address on the user properties.
|
|
50
|
+
* @param properties A dictionary of traits you know about the user like their email, name, plan etc.
|
|
51
|
+
* @param options Options for the identify call such as whether the user is active.
|
|
52
|
+
*/
|
|
53
|
+
identify(properties: Record<string, any>, options?: {
|
|
54
|
+
active?: boolean;
|
|
55
|
+
}): void;
|
|
56
|
+
identify(distinctId: string | number | null, properties?: Record<string, any>, options?: {
|
|
57
|
+
active?: boolean;
|
|
58
|
+
}): void;
|
|
59
|
+
/**
|
|
60
|
+
* Handles sending data to the Saasco Analytics API
|
|
61
|
+
* 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
|
|
62
|
+
* @param path
|
|
63
|
+
* @param data
|
|
64
|
+
*/
|
|
65
|
+
private doRequest;
|
|
66
|
+
/**
|
|
67
|
+
* @param args Arguments to be logged
|
|
68
|
+
*/
|
|
69
|
+
private log;
|
|
70
|
+
private getSearchParam;
|
|
71
|
+
/**
|
|
72
|
+
* If autoPageTracking is enabled, this will automatically track page views
|
|
73
|
+
* It listens to url changes to track new pages every time the url changes
|
|
74
|
+
* @returns
|
|
75
|
+
*/
|
|
76
|
+
private initiAutoPageTracking;
|
|
77
|
+
}
|