Skip to content

Commit

Permalink
Migrating amplitude and crisp plugin/composable
Browse files Browse the repository at this point in the history
  • Loading branch information
JhumanJ committed Dec 9, 2023
1 parent 1f853e8 commit 12778fa
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 51 deletions.
6 changes: 5 additions & 1 deletion client/app.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<template>
<div id="app" class="bg-white dark:bg-notion-dark">
<ServiceCrisp/>
<transition enter-active-class="linear duration-200 overflow-hidden"
enter-from-class="max-h-0"
enter-to-class="max-h-screen"
Expand Down Expand Up @@ -45,12 +44,17 @@ export default {
setup() {
const appStore = useAppStore()
return {
layout: computed(() => appStore.layout),
isIframe: useIsIframe()
}
},
mounted() {
useCrisp().showChat()
},
data: () => ({
metaTitle: 'OpnForm',
metaDescription: 'Create beautiful forms for free. Unlimited fields, unlimited submissions. It\'s free and it takes less than 1 minute to create your first form.',
Expand Down
2 changes: 1 addition & 1 deletion client/components/global/NotionPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
</script>

<style lang="scss">
@import "styles.css";
@import "vue-notion/src/styles.css";
.notion-blue {
@apply text-nt-blue;
Expand Down
32 changes: 0 additions & 32 deletions client/components/service/Crisp.vue

This file was deleted.

57 changes: 42 additions & 15 deletions client/composables/useCrisp.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,80 @@
export const useCrisp = () => {

function push(args) {
if (process.client) {
window.$crisp.push(args)
}
}
let crisp = process.client ? window.Crisp : null

function openChat() {
push(['do', 'chat:open'])
if (!crisp) return
crisp.chat.open()
}

function showChat() {
push(['do', 'chat:show'])
if (!crisp) return
crisp.chat.show()
}

function hideChat() {
push(['do', 'chat:hide'])
if (!crisp) return
crisp.chat.hide()
}

function closeChat() {
push(['do', 'chat:close'])
if (!crisp) return
crisp.chat.close()
}

function openAndShowChat(message = null) {
if (!crisp) return
showChat()
openChat()
if (message) sendTextMessage(message)
}

function openHelpdesk(){
push(['do', 'helpdesk:search'])
if (!crisp) return
openChat()
crisp.chat.setHelpdeskView()
}
function openHelpdeskArticle(articleSlug, lang = 'en') {
push(['do', 'helpdesk:article:open', [lang, articleSlug]])
function openHelpdeskArticle(articleSlug, locale = 'en') {
if (!crisp) return
crisp.chat.openHelpdeskArticle(locale, articleSlug);
}

function sendTextMessage(message) {
push(['do', 'message:send', ['text',message]])
if (!crisp) return
crisp.message.send('text', message)
}

function setUser (user) {
if (!crisp) return
crisp.user.setEmail(user.email);
crisp.user.setNickname(user.name);
crisp.session.setData({
user_id : user.id,
'pro-subscription' : user?.is_subscribed ?? false,
'stripe-id' : user?.stripe_id ?? '',
'subscription' : user?.has_enterprise_subscription ? 'enterprise' : 'pro'
});

if (user?.is_subscribed ?? false) {
setSegments([['subscribed', user?.has_enterprise_subscription ? 'enterprise' : 'pro']])
}
}

function setSegments(segments, overwrite = false) {
if (!crisp) return
crisp.session.setSegments(segments, overwrite)
}

return {
push,
crisp,
openChat,
showChat,
hideChat,
closeChat,
openAndShowChat,
openHelpdesk,
openHelpdeskArticle,
sendTextMessage
sendTextMessage,
setUser
}
}
10 changes: 10 additions & 0 deletions client/plugins/crisp.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Crisp} from "crisp-sdk-web"
import config from "~/opnform.config.js";

export default defineNuxtPlugin(nuxtApp => {
const isIframe = useIsIframe()
if (config.crisp_website_id && !isIframe) {
Crisp.configure(config.crisp_website_id)
window.Crisp = Crisp
}
});
7 changes: 6 additions & 1 deletion client/stores/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const useAuthStore = defineStore('auth', {
try {
const { data } = await axios.get('/api/user')
this.user = data
this.initServiceClients()

return data
} catch (e) {
Expand All @@ -50,7 +51,11 @@ export const useAuthStore = defineStore('auth', {
this.user = payload
},

load
initServiceClients() {
if (!this.user) return
useAmplitude().setUser(this.user)
useCrisp().setUser(this.user)
},

async logout () {
try {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"axios": "^0.21.1",
"chart.js": "^4.4.0",
"clone-deep": "^4.0.1",
"crisp-sdk-web": "^1.0.21",
"date-fns": "^2.28.0",
"debounce": "^1.2.1",
"fuse.js": "^6.4.6",
Expand All @@ -36,8 +37,8 @@
"vue-country-flag-next": "^2.3.2",
"vue-i18n": "^8.25.0",
"vue-meta": "^3.0.0-alpha.2",
"vue-router": "^4.2.5",
"vue-notion": "^3.0.0-beta.1",
"vue-router": "^4.2.5",
"vue-signature-pad": "^3.0.2",
"vue3-editor": "^0.1.1",
"vue3-vt-notifications": "^1.0.0",
Expand Down

0 comments on commit 12778fa

Please sign in to comment.