@frontend-sdk/gorgias
TypeScript icon, indicating that this package has built-in type declarations

0.26.0 • Public • Published

Gorgias

Gorgias integration for Shogun Frontend.

⚠️ This package runs on Shogun Frontend and is in customer Beta. It might not currently support all ecommerce platforms or cover all use cases.

Gorgias website →

Overview

Gorgias is the ecommerce helpdesk that turns your customer service into a profit center.

Installation

yarn add @frontend-sdk/gorgias

npm install @frontend-sdk/gorgias

Connecting to Gorgias Helpdesk

Finding required values

In order to initialize chat widget, we need to find some required values in Gorgias Helpdesk.

First, navigate to "Settings" -> "Integrations" -> "Chat" page in Gorgias Helpdesk and create a new chat or modify the existing one:

Next, navigate to "Installation" tab on chat settings page and find a section with JavaScript code:

Finally, extract these values from the code: GORGIAS_CHAT_APP_ID, GORGIAS_CHAT_BASE_URL and GORGIAS_API_BASE_URL

Now we have everything to initialize the widget in the app.

We need to execute useChat hook and pass extracted values to that hook.

Executing the hook

import { useChat } from '@frontend-sdk/gorgias'

const App = () => {
  // the values below should be extracted from the
  // section with JavaScript code as described above
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return (
    <section>
      <h1>App</h1>
      Chat: {status}
    </section>
  )
}

useChat may be executed not only once per application but also inside local components. The hook will correctly free all allocated resources on unmount. This may be useful if you want to show chat widget only on some pages of the app.

import { useChat } from '@frontend-sdk/gorgias'

const ContactsPage = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return (
    <section>
      <h1>Contacts</h1>
      Chat: {status}
    </section>
  )
}

Manual opening and closing

In some cases it may be useful to have manual control over chat popup visibility state in the code. This package exports useChatState and useChatActions hooks to control it.

Note that these hooks should only be used after useChat returns ready status.

import { useChatActions, useChatState } from '@frontend-sdk/gorgias'

const Chat = () => {
  const { open, close } = useChatActions()
  const { isOpened } = useChatState()
  return (
    <section>
      Chat is {isOpened ? 'opened' : 'closed'}
      <button onClick={open}>Open</button>
      <button onClick={close}>Close</button>
    </section>
  )
}

const App = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return <section>{status === 'ready' && <Chat />}</section>
}

Updating context

Gorgias supports passing extra data (e.g. Shopify context) to the chat. This package exports useChatActions hook for that.

Note that this hook should only be used after useChat returns ready status.

Setting current user email

You can use setUserEmail action to send current user email to Gorgias Helpdesk to identify current chat user. This action can be used together with frontend-customer package.

import { useChat, useChatActions } from '@frontend-sdk/gorgias'
import { useCustomerState } from 'frontend-customer'
import { useEffect } from 'react'

const Chat = () => {
  const { setUserEmail } = useChatActions()
  const { email } = useCustomerState()
  useEffect(() => {
    email && setUserEmail(email)
  }, [email, setUserEmail])
  return null
}

const App = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return <section>{status === 'ready' && <Chat />}</section>
}

Setting Shopify context

Besides user email, you can also use setShopifyContext action to send store's Shopify domain, current customer ID and current Shopify cart. Obviously, this is supported only for Shopify stores. This action can also be used together with frontend-customer package.

import { useChatActions, useChatState, ShopifyCart } from '@frontend-sdk/gorgias'
import { useCustomerState } from 'frontend-customer'
import { useEffect } from 'react'

const Chat = () => {
  const { setShopifyContext } = useChatActions()
  const { id } = useCustomerState()
  const [cart, setCart] = useState<ShopifyCart | undefined>(undefined)

  useEffect(() => {
    // Shogun Frontend stores proxy these requests to Shopify
    fetch('./cart.js')
      .then((response) => response.json())
      .then(setCart)
  }, [])

  const customerId = typeof id === 'number' ? id : undefined

  useEffect(() => {
    setShopifyContext({
      // this should be the public platform domain of your Shopify store
      domain: 'store.myshopify.com',
      // Shopify only supports numeric customer ids
      // while `frontend-customer` returns `number | string | null`
      customerId,
      cart,
    })
  }, [id, cart, customerId])

  return null
}

const App = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return <section>{status === 'ready' && <Chat />}</section>
}

Clearing local data

Gorgias Chat script stores some data in localStorage and it can be useful to clear this. For this purpose, this package exports another action clear in useChatActions hook. This action clears all user data, all Gorgias identifiers including the current chat token, and everything else. Essentially, this action creates a brand new chat.

Note that this hook should only be used after useChat returns ready status.

import { useChat, useChatActions } from '@frontend-sdk/gorgias'
import { useCustomerState } from 'frontend-customer'
import { useEffect } from 'react'

const Chat = () => {
  const { clear } = useChatActions()
  return <button onClick={clear}>Clear</button>
}

const App = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return <section>{status === 'ready' && <Chat />}</section>
}

Dependencies (1)

Dev Dependencies (6)

Package Sidebar

Install

npm i @frontend-sdk/gorgias

Weekly Downloads

2

Version

0.26.0

License

MIT

Unpacked Size

31.9 kB

Total Files

17

Last publish

Collaborators

  • facundoshogun
  • georgy-nemtsov
  • edengetshogun
  • shogun-engineering
  • nino-majder
  • william-shogun
  • ivins
  • shogun-admin