npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@frontend-sdk/gorgias

v0.26.0

Published

Gorgias integration for Shogun Frontend.

Downloads

82

Readme

Gorgias

Gorgias integration for Shogun Frontend.

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>
}