@hellotext/hellotext 1.0.5 → 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 +48 -29
- package/package.json +1 -5
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,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { NotInitializedError } from "./errors/notInitializedError";
|
|
2
2
|
|
|
3
3
|
const apiUrl = 'https://api.hellotext.com/v1/'
|
|
4
4
|
|
|
@@ -17,8 +17,8 @@ class Hellotext {
|
|
|
17
17
|
this.setSessionCookie(session)
|
|
18
18
|
} else {
|
|
19
19
|
this.mintAnonymousSession().then(response => {
|
|
20
|
-
this._session = response.
|
|
21
|
-
this.setSessionCookie(response.
|
|
20
|
+
this._session = response.id
|
|
21
|
+
this.setSessionCookie(response.id)
|
|
22
22
|
})
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -30,53 +30,72 @@ class Hellotext {
|
|
|
30
30
|
* @param { String } url optional url, the url is automatically inferred when the action is tracked
|
|
31
31
|
* @returns {Promise}
|
|
32
32
|
*/
|
|
33
|
-
static track(action, params, url = null) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
static async track(action, params, url = null) {
|
|
34
|
+
if(this.notInitialized) { throw new NotInitializedError() }
|
|
35
|
+
|
|
36
|
+
const response = await fetch(apiUrl + "track/events", {
|
|
37
|
+
headers: this.headers,
|
|
38
|
+
method: "post",
|
|
39
|
+
body: JSON.stringify({
|
|
37
40
|
session: this.session,
|
|
38
41
|
url: url || window.location.href,
|
|
39
42
|
action,
|
|
40
43
|
...params,
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
)
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return response.json()
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* @returns {Promise<any>|String}
|
|
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 => {
|
|
54
|
-
this._session = response.
|
|
55
|
-
return response.
|
|
56
|
-
})
|
|
60
|
+
this._session = response.id
|
|
61
|
+
return response.id
|
|
62
|
+
}).then(() => this._session)
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
// private
|
|
60
66
|
|
|
61
|
-
static mintAnonymousSession() {
|
|
67
|
+
static async mintAnonymousSession() {
|
|
68
|
+
if(this.notInitialized) { throw new NotInitializedError() }
|
|
69
|
+
|
|
62
70
|
const trackingUrl = apiUrl + 'track/sessions'
|
|
63
71
|
|
|
64
|
-
this.mintingPromise =
|
|
65
|
-
|
|
66
|
-
{},
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
this.mintingPromise = await fetch(trackingUrl, {
|
|
73
|
+
method: "post",
|
|
74
|
+
headers: { Authorization: `Bearer ${this.business_id}` },
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
return this.mintingPromise.json()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static get headers() {
|
|
81
|
+
if(this.notInitialized) { throw new NotInitializedError() }
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
Authorization: `Bearer ${this.business_id}`,
|
|
85
|
+
Accept: 'application.json',
|
|
86
|
+
'Content-Type': 'application/json'
|
|
87
|
+
}
|
|
75
88
|
}
|
|
76
89
|
|
|
77
90
|
static setSessionCookie(session) {
|
|
91
|
+
if(this.notInitialized) { throw new NotInitializedError() }
|
|
92
|
+
|
|
78
93
|
document.cookie = `hello_session=${session}`
|
|
79
94
|
}
|
|
95
|
+
|
|
96
|
+
static get notInitialized() {
|
|
97
|
+
return this.business_id === undefined
|
|
98
|
+
}
|
|
80
99
|
}
|
|
81
100
|
|
|
82
101
|
const getCookieValue = name => document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop()
|
package/package.json
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hellotext/hellotext",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Hellotext javascript client",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Hellotext",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"homepage": "https://github.com/hellotext/hellotext.js",
|
|
9
|
-
"dependencies": {
|
|
10
|
-
"@rails/actioncable": "^7.0.4",
|
|
11
|
-
"axios": "^0.21.1"
|
|
12
|
-
},
|
|
13
9
|
"devDependencies": {
|
|
14
10
|
"prettier": "2.8.2"
|
|
15
11
|
},
|