@hellotext/hellotext 1.0.7 → 1.0.9
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 +4 -4
- package/lib/errors/notInitializedError.js +10 -0
- package/lib/hellotext.js +27 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# Hellotext.js
|
|
2
2
|
|
|
3
|
-
[](https://badge.fury.io/js/@hellotext%2Fhellotext)
|
|
4
|
-
|
|
5
3
|
Track the events happening on your site to [Hellotext](https://www.hellotext.com) in real-time with this library.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
@@ -34,6 +32,8 @@ You can find it from the business's settings page.
|
|
|
34
32
|
Hellotext.initialize("HELLOTEXT_BUSINESS_ID");
|
|
35
33
|
```
|
|
36
34
|
|
|
35
|
+
Failing to initialize the class before calling any other method will throw a `NotInitializedError`.
|
|
36
|
+
|
|
37
37
|
## Usage
|
|
38
38
|
|
|
39
39
|
Tracking events is straightforward and perhaps the simplest example is tracking a page view:
|
|
@@ -55,7 +55,7 @@ You can create the associated object directly by defining its attributes in a ha
|
|
|
55
55
|
Hellotext.track("order.placed", {
|
|
56
56
|
amount: 395.00,
|
|
57
57
|
currency: "USD",
|
|
58
|
-
|
|
58
|
+
order_attributes: {
|
|
59
59
|
"amount": "395.00",
|
|
60
60
|
"reference": "654321",
|
|
61
61
|
}
|
|
@@ -135,7 +135,7 @@ It is possible to obtain the current session by simply calling `Hellotext.sessio
|
|
|
135
135
|
|
|
136
136
|
```javascript
|
|
137
137
|
Hellotext.session
|
|
138
|
-
// Returns
|
|
138
|
+
// Returns bBJn9vR15yPaYkWmR2QK0jopMeNxrA6l
|
|
139
139
|
```
|
|
140
140
|
|
|
141
141
|
You may want to store the session on your backend when customers are unidentified so you can later [attach it to a profile](https://www.hellotext.com/api#attach_session) when it becomes known.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class NotInitializedError extends Error {
|
|
2
|
+
constructor() {
|
|
3
|
+
super(
|
|
4
|
+
'You need to initialize before tracking events. Call Hellotext.initialize and pass your public business id',
|
|
5
|
+
)
|
|
6
|
+
this.name = 'NotInitializedError'
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { NotInitializedError }
|
package/lib/hellotext.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { NotInitializedError } from './errors/notInitializedError'
|
|
2
|
+
|
|
1
3
|
const apiUrl = 'https://api.hellotext.com/v1/'
|
|
2
4
|
|
|
3
5
|
class Hellotext {
|
|
@@ -29,15 +31,17 @@ class Hellotext {
|
|
|
29
31
|
* @returns {Promise}
|
|
30
32
|
*/
|
|
31
33
|
static async track(action, params, url = null) {
|
|
32
|
-
|
|
34
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
35
|
+
|
|
36
|
+
const response = await fetch(apiUrl + 'track/events', {
|
|
33
37
|
headers: this.headers,
|
|
34
|
-
method:
|
|
38
|
+
method: 'post',
|
|
35
39
|
body: JSON.stringify({
|
|
36
40
|
session: this.session,
|
|
37
41
|
url: url || window.location.href,
|
|
38
42
|
action,
|
|
39
43
|
...params,
|
|
40
|
-
})
|
|
44
|
+
}),
|
|
41
45
|
})
|
|
42
46
|
|
|
43
47
|
return response.json()
|
|
@@ -48,21 +52,27 @@ class Hellotext {
|
|
|
48
52
|
* @returns {Promise<any>|String}
|
|
49
53
|
*/
|
|
50
54
|
static get session() {
|
|
55
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
56
|
+
|
|
51
57
|
if (this._session) return this._session
|
|
52
58
|
|
|
53
|
-
return this.mintAnonymousSession()
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
return this.mintAnonymousSession()
|
|
60
|
+
.then(response => {
|
|
61
|
+
this._session = response.id
|
|
62
|
+
return response.id
|
|
63
|
+
})
|
|
64
|
+
.then(() => this._session)
|
|
57
65
|
}
|
|
58
66
|
|
|
59
67
|
// private
|
|
60
68
|
|
|
61
69
|
static async mintAnonymousSession() {
|
|
70
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
71
|
+
|
|
62
72
|
const trackingUrl = apiUrl + 'track/sessions'
|
|
63
73
|
|
|
64
74
|
this.mintingPromise = await fetch(trackingUrl, {
|
|
65
|
-
method:
|
|
75
|
+
method: 'post',
|
|
66
76
|
headers: { Authorization: `Bearer ${this.business_id}` },
|
|
67
77
|
})
|
|
68
78
|
|
|
@@ -70,16 +80,24 @@ class Hellotext {
|
|
|
70
80
|
}
|
|
71
81
|
|
|
72
82
|
static get headers() {
|
|
83
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
84
|
+
|
|
73
85
|
return {
|
|
74
86
|
Authorization: `Bearer ${this.business_id}`,
|
|
75
87
|
Accept: 'application.json',
|
|
76
|
-
'Content-Type': 'application/json'
|
|
88
|
+
'Content-Type': 'application/json',
|
|
77
89
|
}
|
|
78
90
|
}
|
|
79
91
|
|
|
80
92
|
static setSessionCookie(session) {
|
|
93
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
94
|
+
|
|
81
95
|
document.cookie = `hello_session=${session}`
|
|
82
96
|
}
|
|
97
|
+
|
|
98
|
+
static get notInitialized() {
|
|
99
|
+
return this.business_id === undefined
|
|
100
|
+
}
|
|
83
101
|
}
|
|
84
102
|
|
|
85
103
|
const getCookieValue = name => document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop()
|