2021-09-21 12:47:14 +02:00
|
|
|
import posthog from "posthog-js"
|
|
|
|
import { Events } from "./constants"
|
|
|
|
|
|
|
|
export default class PosthogClient {
|
|
|
|
constructor(token, url) {
|
|
|
|
this.token = token
|
|
|
|
this.url = url
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
if (!this.token || !this.url) return
|
|
|
|
|
|
|
|
posthog.init(this.token, {
|
|
|
|
autocapture: false,
|
|
|
|
capture_pageview: false,
|
|
|
|
api_host: this.url,
|
|
|
|
})
|
|
|
|
posthog.set_config({ persistence: "cookie" })
|
|
|
|
|
|
|
|
this.initialised = true
|
|
|
|
}
|
|
|
|
|
|
|
|
identify(id) {
|
|
|
|
if (!this.initialised) return
|
|
|
|
|
|
|
|
posthog.identify(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUser(meta) {
|
|
|
|
if (!this.initialised) return
|
|
|
|
|
|
|
|
posthog.people.set(meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
captureException(err) {
|
|
|
|
if (!this.initialised) return
|
|
|
|
|
|
|
|
this.captureEvent("Error", { error: err.message ? err.message : err })
|
|
|
|
}
|
|
|
|
|
|
|
|
captureEvent(eventName, props) {
|
|
|
|
if (!this.initialised) return
|
|
|
|
|
|
|
|
props.sourceApp = "builder"
|
|
|
|
posthog.capture(eventName, props)
|
|
|
|
}
|
|
|
|
|
|
|
|
npsFeedback(values) {
|
|
|
|
if (!this.initialised) return
|
|
|
|
|
|
|
|
localStorage.setItem(Events.NPS.SUBMITTED, Date.now())
|
|
|
|
|
|
|
|
const prefixedFeedback = {}
|
|
|
|
for (let key in values) {
|
|
|
|
prefixedFeedback[`feedback_${key}`] = values[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
posthog.capture(Events.NPS.SUBMITTED, prefixedFeedback)
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:21:15 +02:00
|
|
|
logout() {
|
|
|
|
if (!this.initialised) return
|
|
|
|
|
|
|
|
posthog.reset()
|
|
|
|
}
|
|
|
|
}
|