@hellotext/hellotext 1.0.1
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/.prettierrc.json +18 -0
- package/MIT-LICENSE +20 -0
- package/README.md +139 -0
- package/index.js +3 -0
- package/lib/hellotext.js +84 -0
- package/package.json +30 -0
package/.prettierrc.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"arrowParens": "avoid",
|
|
3
|
+
"bracketSpacing": true,
|
|
4
|
+
"printWidth": 100,
|
|
5
|
+
"semi": false,
|
|
6
|
+
"singleQuote": true,
|
|
7
|
+
"tabWidth": 2,
|
|
8
|
+
"trailingComma": "all",
|
|
9
|
+
"useTabs": false,
|
|
10
|
+
"overrides": [
|
|
11
|
+
{
|
|
12
|
+
"files": "*.json",
|
|
13
|
+
"options": {
|
|
14
|
+
"printWidth": 200
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
package/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2022 HelloText
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Hellotext
|
|
2
|
+
|
|
3
|
+
Track the events happening on your site to [Hellotext](https://www.hellotext.com) in real-time with this library.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### Using NPM
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i @hellotext/hellotext
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Using yarn
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @hellotext/hellotext
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Configure
|
|
20
|
+
|
|
21
|
+
Import the library into your app.
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
import Hellotext from "hellotext";
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Initialize the library passing the public `HELLOTEXT_BUSINESS_ID` identifier that represents the business.
|
|
28
|
+
|
|
29
|
+
You can find it from the business's settings page.
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
Hellotext.initialize("HELLOTEXT_BUSINESS_ID");
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
Tracking events is straightforward and perhaps the simplest example is tracking a page view:
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
Hellotext.track("page.viewed");
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
In the example above only the name of the action is required.
|
|
44
|
+
The library takes care of handling the `url` parameter with the current URL automatically and is not required to specify it explicitly.
|
|
45
|
+
You can pass another url as the third argument to the `Hellotext.track` method.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
Generally, most actions also require an associated object. These can be of type [`app`](https://www.hellotext.com/api#apps), [`coupon`](https://www.hellotext.com/api#coupons), [`form`](https://www.hellotext.com/api#forms), [`order`](https://www.hellotext.com/api#orders), [`product`](https://www.hellotext.com/api#products) and [`refund`](https://www.hellotext.com/api#refunds).
|
|
49
|
+
|
|
50
|
+
You can create the associated object directly by defining its attributes in a hash:
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
Hellotext.track("order.placed", {
|
|
54
|
+
amount: 395.00,
|
|
55
|
+
currency: "USD",
|
|
56
|
+
order: {
|
|
57
|
+
"amount": "395.00",
|
|
58
|
+
"reference": "654321",
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
If you want to reuse existing objects, you must pass the identifier of an existing associated object. For example, to track a product purchase the identifier of a previously created product object as the `product`.
|
|
64
|
+
For more information about identifiers, view the [Tracking API](https://www.hellotext.com/api#tracking)
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
Hellotext.track("product.purchased", {
|
|
68
|
+
amount: 395.00,
|
|
69
|
+
currency: "USD",
|
|
70
|
+
product: "erA2RAXE"
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## List of actions
|
|
75
|
+
|
|
76
|
+
The following is a complete list of built-in actions and their required associated objects.
|
|
77
|
+
|
|
78
|
+
| Action | Description | Required Parameter |
|
|
79
|
+
|-----------------------| --- | --- |
|
|
80
|
+
| **app.installed** | An app was installed. | `app` or [app_attributes](https://www.hellotext.com/api#app)
|
|
81
|
+
| **app.removed** | An app was removed. | `app` or [app_attributes](https://www.hellotext.com/api#app)
|
|
82
|
+
| **app.spent** | A customer spent on an app. | `app` or [app_attributes](https://www.hellotext.com/api#app)
|
|
83
|
+
| **cart.added** | Added an item to the cart. | `product` or [product_attributes](https://www.hellotext.com/api#products)
|
|
84
|
+
| **cart.removed** | Removed an item from the cart. | `product` or [product_attributes](https://www.hellotext.com/api#products)
|
|
85
|
+
| **coupon.redeemed** | A coupon was redeem by a customer. | `coupon` or [coupon_attributes](https://www.hellotext.com/api#coupons)
|
|
86
|
+
| **form.completed** | A form was completed by the customer. | `form` or [form_attributes](https://www.hellotext.com/api#forms)
|
|
87
|
+
| **order.placed** | Order has been placed. | `order` or [order_attributes](https://www.hellotext.com/api#orders)
|
|
88
|
+
| **order.confirmed** | Order has been confirmed by you. | `order` or [order_attributes](https://www.hellotext.com/api#orders)
|
|
89
|
+
| **order.cancelled** | Order has been cancelled either by you or your customer. | `order` or [order_attributes](https://www.hellotext.com/api#orders)
|
|
90
|
+
| **order.shipped** | Order has been shipped to your customer. | `order` or [order_attributes](https://www.hellotext.com/api#orders)
|
|
91
|
+
| **page.viewed** | A page was viewed by a customer. | `url`
|
|
92
|
+
| **product.purchased** | A product has been purchased. | `product` or [product_attributes](https://www.hellotext.com/api#products)
|
|
93
|
+
| **product.viewed** | A product page has been viewed. | `product` or [product_attributes](https://www.hellotext.com/api#products)
|
|
94
|
+
| **refund.requested** | A customer requested a refund. | `refund` or [refund_attributes](https://www.hellotext.com/api#refunds)
|
|
95
|
+
| **refund.received** | A refund was issued by you to your customer. | `refund` or [refund_attributes](https://www.hellotext.com/api#refunds)
|
|
96
|
+
|
|
97
|
+
You can also create your **[own defined actions](https://www.hellotext.com/api#actions)**.
|
|
98
|
+
|
|
99
|
+
## Additional Properties
|
|
100
|
+
|
|
101
|
+
You can include additional attributes to the tracked event, additional properties must be included inside the `metadata` object:
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
Hellotext.track("product.purchased", {
|
|
105
|
+
amount: 0.20,
|
|
106
|
+
currency: "USD",
|
|
107
|
+
metadata: {
|
|
108
|
+
myProperty: "custom"
|
|
109
|
+
},
|
|
110
|
+
tracked_at: 1665684173
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### List of additional attributes
|
|
115
|
+
|
|
116
|
+
| Property | Description | Type | Default |
|
|
117
|
+
| --- | --- | --- | --- |
|
|
118
|
+
| **amount** | Monetary amount that represents the revenue associated to this tracked event. | float | `null`
|
|
119
|
+
| **currency** | Currency for the `amount` given in ISO 4217 format. | currency | `USD`
|
|
120
|
+
| **metadata** | Set of key-value pairs that you can attach to an event. This can be useful for storing additional information about the object in a structured format. | hash | `{}`
|
|
121
|
+
| **tracked_at** | Original date when the event happened. This is useful if you want to record an event that happened in the past. If no value is provided its value will be the same from `created_at`. | epoch | `null`
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
## Understanding Sessions
|
|
125
|
+
|
|
126
|
+
The library looks for a session identifier present on the `hellotext_session_id` parameter. If the session is not present as a cookie neither it will create a new random session identifier. The session is automatically sent to Hellotext any time the `Hellotext.track` method is called.
|
|
127
|
+
|
|
128
|
+
Short links redirections attaches a session identifier to the destination url as `hellotext_session_id` parameter. This will identify all the events back to the customer who opened the link.
|
|
129
|
+
|
|
130
|
+
### Get session
|
|
131
|
+
|
|
132
|
+
It is possible to obtain the current session by simply calling `Hellotext.session`.
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
Hellotext.session
|
|
136
|
+
// Returns c7a42761-f34d-41a2-b078-6a8172690350
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
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/index.js
ADDED
package/lib/hellotext.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
|
|
3
|
+
const apiUrl = 'https://api.hellotext.com/v1/'
|
|
4
|
+
|
|
5
|
+
class Hellotext {
|
|
6
|
+
/**
|
|
7
|
+
* initialize the module.
|
|
8
|
+
*/
|
|
9
|
+
static initialize(business_id) {
|
|
10
|
+
this.business_id = business_id
|
|
11
|
+
|
|
12
|
+
const urlSearchParams = new URLSearchParams(window.location.search)
|
|
13
|
+
const session = urlSearchParams.get('hello_session_id') || getCookieValue('hello_session_id')
|
|
14
|
+
|
|
15
|
+
if (session) {
|
|
16
|
+
this._session = session
|
|
17
|
+
this.setSessionCookie(session)
|
|
18
|
+
} else {
|
|
19
|
+
this.mintAnonymousSession().then(response => {
|
|
20
|
+
this._session = response.data
|
|
21
|
+
this.setSessionCookie(response.data)
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param { String } action a valid action name
|
|
29
|
+
* @param { Object } params
|
|
30
|
+
* @param { String } url optional url, the url is automatically inferred when the action is tracked
|
|
31
|
+
* @returns {Promise}
|
|
32
|
+
*/
|
|
33
|
+
static track(action, params, url = null) {
|
|
34
|
+
return axios.post(
|
|
35
|
+
apiUrl + 'track/events',
|
|
36
|
+
{
|
|
37
|
+
session: this.session,
|
|
38
|
+
url: url || window.location.href,
|
|
39
|
+
action,
|
|
40
|
+
...params,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
headers: {
|
|
44
|
+
Authorization: `Bearer ${this.business_id}`,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static get session() {
|
|
51
|
+
if (this._session) return this._session
|
|
52
|
+
|
|
53
|
+
return this.mintAnonymousSession().then(response => {
|
|
54
|
+
this._session = response.data
|
|
55
|
+
return response.data
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// private
|
|
60
|
+
|
|
61
|
+
static mintAnonymousSession() {
|
|
62
|
+
const trackingUrl = apiUrl + 'track/sessions'
|
|
63
|
+
|
|
64
|
+
this.mintingPromise = axios.post(
|
|
65
|
+
trackingUrl,
|
|
66
|
+
{},
|
|
67
|
+
{
|
|
68
|
+
headers: {
|
|
69
|
+
Authorization: `Bearer ${this.business_id}`,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
return this.mintingPromise
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static setSessionCookie(session) {
|
|
78
|
+
document.cookie = `hello_session_id=${session}`
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const getCookieValue = name => document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop()
|
|
83
|
+
|
|
84
|
+
export default Hellotext
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hellotext/hellotext",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Hellotext javascript client",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"author": "Hellotext",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/hellotext/hellotext.js",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@rails/actioncable": "^7.0.4",
|
|
11
|
+
"axios": "^0.21.1"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"prettier": "2.8.2"
|
|
15
|
+
},
|
|
16
|
+
"directories": {
|
|
17
|
+
"lib": "lib"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/hellotext/hellotext.js.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"hellotext",
|
|
25
|
+
"javascript"
|
|
26
|
+
],
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/hellotext/hellotext.js/issues"
|
|
29
|
+
}
|
|
30
|
+
}
|