@hellotext/hellotext 2.2.1 → 2.3.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/src/hellotext.js CHANGED
@@ -1,14 +1,11 @@
1
1
  import { Configuration, Event } from './core'
2
2
 
3
3
  import API, { Response } from './api'
4
- import { Business, FormCollection, Page, Query, Session, Webchat } from './models'
4
+ import { Business, FormCollection, Page, Query, Session, User, Webchat } from './models'
5
5
 
6
6
  import { NotInitializedError } from './errors'
7
7
 
8
8
  class Hellotext {
9
- static #session
10
- static #query
11
-
12
9
  static eventEmitter = new Event()
13
10
  static forms
14
11
  static business
@@ -27,7 +24,7 @@ class Hellotext {
27
24
  this.business = new Business(business)
28
25
  this.forms = new FormCollection()
29
26
 
30
- this.#query = new Query()
27
+ this.query = new Query()
31
28
 
32
29
  if (Configuration.webchat.id) {
33
30
  this.webchat = await Webchat.load(Configuration.webchat.id)
@@ -59,6 +56,7 @@ class Hellotext {
59
56
 
60
57
  const body = {
61
58
  session: this.session,
59
+ user: User.identificationData,
62
60
  action,
63
61
  ...params,
64
62
  ...pageInstance.trackingData,
@@ -72,6 +70,48 @@ class Hellotext {
72
70
  })
73
71
  }
74
72
 
73
+ /**
74
+ * @typedef { Object } IdentificationOptions
75
+ * @property { String } [email] - the email of the user
76
+ * @property { String } [phone] - the phone number of the user
77
+ * @property { String } [name] - the name of the user
78
+ * @property { String } [source] - the platform specific identifier where this pixel is running on.
79
+ *
80
+ * Identifies a user and attaches the hello_session to the user ID
81
+ * @param { String } externalId - the user ID
82
+ * @param { IdentificationOptions } options - the options for the identification
83
+ * @returns {Promise<Response>}
84
+ */
85
+ static async identify(externalId, options = {}) {
86
+ if (User.id === externalId) {
87
+ return new Response(true, {
88
+ json: async () => {
89
+ already_identified: true
90
+ },
91
+ })
92
+ }
93
+
94
+ const response = await API.identifications.create({
95
+ user_id: externalId,
96
+ ...options,
97
+ })
98
+
99
+ if (response.succeeded) {
100
+ User.remember(externalId, options.source)
101
+ }
102
+
103
+ return response
104
+ }
105
+
106
+ /**
107
+ * Clears the user session, use when the user logs out to clear the hello cookies
108
+ *
109
+ * @returns {void}
110
+ */
111
+ static forget() {
112
+ User.forget()
113
+ }
114
+
75
115
  /**
76
116
  * Registers an event listener
77
117
  * @param event the name of the event to listen to
@@ -33,6 +33,19 @@ class Cookies {
33
33
  return undefined
34
34
  }
35
35
  }
36
+
37
+ static delete(name) {
38
+ if (typeof document !== 'undefined') {
39
+ const domain = Page.getRootDomain()
40
+ const secure = window.location.protocol === 'https:' ? '; Secure' : ''
41
+
42
+ if (domain) {
43
+ document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT${secure}; domain=${domain}; SameSite=Lax`
44
+ } else {
45
+ document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT${secure}; SameSite=Lax`
46
+ }
47
+ }
48
+ }
36
49
  }
37
50
 
38
51
  export { Cookies }
@@ -5,5 +5,6 @@ export { FormCollection } from './form_collection'
5
5
  export { Page } from './page'
6
6
  export { Query } from './query'
7
7
  export { Session } from './session'
8
+ export { User } from './user'
8
9
  export { UTM } from './utm'
9
10
  export { Webchat } from './webchat'
@@ -0,0 +1,35 @@
1
+ import { Cookies } from './cookies'
2
+
3
+ class User {
4
+ static get id() {
5
+ return Cookies.get('hello_user_id')
6
+ }
7
+
8
+ static get source() {
9
+ return Cookies.get('hello_user_source')
10
+ }
11
+
12
+ static remember(id, source) {
13
+ if (source) {
14
+ Cookies.set('hello_user_source', source)
15
+ }
16
+
17
+ Cookies.set('hello_user_id', id)
18
+ }
19
+
20
+ static forget() {
21
+ Cookies.delete('hello_user_id')
22
+ Cookies.delete('hello_user_source')
23
+ }
24
+
25
+ static get identificationData() {
26
+ if (!this.id) return {}
27
+
28
+ return {
29
+ id: this.id,
30
+ source: this.source,
31
+ }
32
+ }
33
+ }
34
+
35
+ export { User }