@hellotext/hellotext 1.0.7 → 1.0.8
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 -2
- package/lib/errors/notInitializedError.js +8 -0
- package/lib/hellotext.js +16 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,6 +34,8 @@ You can find it from the business's settings page.
|
|
|
34
34
|
Hellotext.initialize("HELLOTEXT_BUSINESS_ID");
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
+
Failing to initialize the class before calling any other method will throw a `NotInitializedError`.
|
|
38
|
+
|
|
37
39
|
## Usage
|
|
38
40
|
|
|
39
41
|
Tracking events is straightforward and perhaps the simplest example is tracking a page view:
|
|
@@ -55,7 +57,7 @@ You can create the associated object directly by defining its attributes in a ha
|
|
|
55
57
|
Hellotext.track("order.placed", {
|
|
56
58
|
amount: 395.00,
|
|
57
59
|
currency: "USD",
|
|
58
|
-
|
|
60
|
+
order_attributes: {
|
|
59
61
|
"amount": "395.00",
|
|
60
62
|
"reference": "654321",
|
|
61
63
|
}
|
|
@@ -135,7 +137,7 @@ It is possible to obtain the current session by simply calling `Hellotext.sessio
|
|
|
135
137
|
|
|
136
138
|
```javascript
|
|
137
139
|
Hellotext.session
|
|
138
|
-
// Returns
|
|
140
|
+
// Returns bBJn9vR15yPaYkWmR2QK0jopMeNxrA6l
|
|
139
141
|
```
|
|
140
142
|
|
|
141
143
|
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.
|
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,6 +31,8 @@ class Hellotext {
|
|
|
29
31
|
* @returns {Promise}
|
|
30
32
|
*/
|
|
31
33
|
static async track(action, params, url = null) {
|
|
34
|
+
if(this.notInitialized) { throw new NotInitializedError() }
|
|
35
|
+
|
|
32
36
|
const response = await fetch(apiUrl + "track/events", {
|
|
33
37
|
headers: this.headers,
|
|
34
38
|
method: "post",
|
|
@@ -48,6 +52,8 @@ 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
59
|
return this.mintAnonymousSession().then(response => {
|
|
@@ -59,6 +65,8 @@ class Hellotext {
|
|
|
59
65
|
// private
|
|
60
66
|
|
|
61
67
|
static async mintAnonymousSession() {
|
|
68
|
+
if(this.notInitialized) { throw new NotInitializedError() }
|
|
69
|
+
|
|
62
70
|
const trackingUrl = apiUrl + 'track/sessions'
|
|
63
71
|
|
|
64
72
|
this.mintingPromise = await fetch(trackingUrl, {
|
|
@@ -70,6 +78,8 @@ class Hellotext {
|
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
static get headers() {
|
|
81
|
+
if(this.notInitialized) { throw new NotInitializedError() }
|
|
82
|
+
|
|
73
83
|
return {
|
|
74
84
|
Authorization: `Bearer ${this.business_id}`,
|
|
75
85
|
Accept: 'application.json',
|
|
@@ -78,8 +88,14 @@ class Hellotext {
|
|
|
78
88
|
}
|
|
79
89
|
|
|
80
90
|
static setSessionCookie(session) {
|
|
91
|
+
if(this.notInitialized) { throw new NotInitializedError() }
|
|
92
|
+
|
|
81
93
|
document.cookie = `hello_session=${session}`
|
|
82
94
|
}
|
|
95
|
+
|
|
96
|
+
static get notInitialized() {
|
|
97
|
+
return this.business_id === undefined
|
|
98
|
+
}
|
|
83
99
|
}
|
|
84
100
|
|
|
85
101
|
const getCookieValue = name => document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop()
|