@hellotext/hellotext 1.0.8 → 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 +0 -2
- package/lib/errors/notInitializedError.js +4 -2
- package/lib/hellotext.js +17 -15
- 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
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
class NotInitializedError extends Error {
|
|
2
2
|
constructor() {
|
|
3
|
-
super(
|
|
4
|
-
|
|
3
|
+
super(
|
|
4
|
+
'You need to initialize before tracking events. Call Hellotext.initialize and pass your public business id',
|
|
5
|
+
)
|
|
6
|
+
this.name = 'NotInitializedError'
|
|
5
7
|
}
|
|
6
8
|
}
|
|
7
9
|
|
package/lib/hellotext.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NotInitializedError } from
|
|
1
|
+
import { NotInitializedError } from './errors/notInitializedError'
|
|
2
2
|
|
|
3
3
|
const apiUrl = 'https://api.hellotext.com/v1/'
|
|
4
4
|
|
|
@@ -31,17 +31,17 @@ class Hellotext {
|
|
|
31
31
|
* @returns {Promise}
|
|
32
32
|
*/
|
|
33
33
|
static async track(action, params, url = null) {
|
|
34
|
-
if(this.notInitialized) { throw new NotInitializedError() }
|
|
34
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
35
35
|
|
|
36
|
-
const response = await fetch(apiUrl +
|
|
36
|
+
const response = await fetch(apiUrl + 'track/events', {
|
|
37
37
|
headers: this.headers,
|
|
38
|
-
method:
|
|
38
|
+
method: 'post',
|
|
39
39
|
body: JSON.stringify({
|
|
40
40
|
session: this.session,
|
|
41
41
|
url: url || window.location.href,
|
|
42
42
|
action,
|
|
43
43
|
...params,
|
|
44
|
-
})
|
|
44
|
+
}),
|
|
45
45
|
})
|
|
46
46
|
|
|
47
47
|
return response.json()
|
|
@@ -52,25 +52,27 @@ class Hellotext {
|
|
|
52
52
|
* @returns {Promise<any>|String}
|
|
53
53
|
*/
|
|
54
54
|
static get session() {
|
|
55
|
-
if(this.notInitialized) { throw new NotInitializedError() }
|
|
55
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
56
56
|
|
|
57
57
|
if (this._session) return this._session
|
|
58
58
|
|
|
59
|
-
return this.mintAnonymousSession()
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
return this.mintAnonymousSession()
|
|
60
|
+
.then(response => {
|
|
61
|
+
this._session = response.id
|
|
62
|
+
return response.id
|
|
63
|
+
})
|
|
64
|
+
.then(() => this._session)
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
// private
|
|
66
68
|
|
|
67
69
|
static async mintAnonymousSession() {
|
|
68
|
-
if(this.notInitialized) { throw new NotInitializedError() }
|
|
70
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
69
71
|
|
|
70
72
|
const trackingUrl = apiUrl + 'track/sessions'
|
|
71
73
|
|
|
72
74
|
this.mintingPromise = await fetch(trackingUrl, {
|
|
73
|
-
method:
|
|
75
|
+
method: 'post',
|
|
74
76
|
headers: { Authorization: `Bearer ${this.business_id}` },
|
|
75
77
|
})
|
|
76
78
|
|
|
@@ -78,17 +80,17 @@ class Hellotext {
|
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
static get headers() {
|
|
81
|
-
if(this.notInitialized) { throw new NotInitializedError() }
|
|
83
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
82
84
|
|
|
83
85
|
return {
|
|
84
86
|
Authorization: `Bearer ${this.business_id}`,
|
|
85
87
|
Accept: 'application.json',
|
|
86
|
-
'Content-Type': 'application/json'
|
|
88
|
+
'Content-Type': 'application/json',
|
|
87
89
|
}
|
|
88
90
|
}
|
|
89
91
|
|
|
90
92
|
static setSessionCookie(session) {
|
|
91
|
-
if(this.notInitialized) { throw new NotInitializedError() }
|
|
93
|
+
if (this.notInitialized) { throw new NotInitializedError() }
|
|
92
94
|
|
|
93
95
|
document.cookie = `hello_session=${session}`
|
|
94
96
|
}
|