From 8833c960ea5572d7ba9c44d7c70fd7cac5863bb0 Mon Sep 17 00:00:00 2001 From: Rahul Mohan Date: Tue, 22 Nov 2022 15:16:58 +0530 Subject: [PATCH 1/2] [3.3.2][TE-6165]: Use same Freshchat session in app and website --- gatsby-config.js | 3 +- package.json | 2 +- .../gatsby-plugin-ts-freshchat/gatsby-ssr.js | 73 +++++++++++++++++++ plugins/gatsby-plugin-ts-freshchat/index.js | 1 + .../gatsby-plugin-ts-freshchat/package.json | 6 ++ src/templates/page.jsx | 2 +- 6 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 plugins/gatsby-plugin-ts-freshchat/gatsby-ssr.js create mode 100644 plugins/gatsby-plugin-ts-freshchat/index.js create mode 100644 plugins/gatsby-plugin-ts-freshchat/package.json diff --git a/gatsby-config.js b/gatsby-config.js index ed720f0..74ec736 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -100,10 +100,11 @@ module.exports = { }, }, { - resolve: `gatsby-plugin-freshchat`, + resolve: `gatsby-plugin-ts-freshchat`, options: { token: process.env.FRESHCHAT_TOKEN, host: "https://wchat.freshchat.com", + appEmbedUrl: "https://prestaging.testsigma.com/ui/dashboard", }, }, { diff --git a/package.json b/package.json index 33c259a..114d479 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "dotenv": "^10.0.0", "gatsby": "^3.13.0", "gatsby-plugin-algolia": "^0.22.2", - "gatsby-plugin-freshchat": "^1.0.0-rc1", + "gatsby-plugin-ts-freshchat": "file:plugins/gatsby-plugin-ts-freshchat", "gatsby-plugin-google-analytics": "^4.14.0", "gatsby-plugin-google-gtag": "^4.4.0", "gatsby-plugin-image": "^1.13.0", diff --git a/plugins/gatsby-plugin-ts-freshchat/gatsby-ssr.js b/plugins/gatsby-plugin-ts-freshchat/gatsby-ssr.js new file mode 100644 index 0000000..619331f --- /dev/null +++ b/plugins/gatsby-plugin-ts-freshchat/gatsby-ssr.js @@ -0,0 +1,73 @@ +const React = require("react") + +module.exports.onRenderBody = ( + {setPostBodyComponents}, + pluginOptions +) => { + if (typeof pluginOptions.token === `undefined`) { + return null + } + + const freshchatHost = pluginOptions.host ? pluginOptions.host : `https://wchat.freshchat.com` + + const appEmbedUrl = + pluginOptions.appEmbedUrl ? pluginOptions.appEmbedUrl : `https://app.testsigma.com/ui/dashboard` + + const renderHTML = () => ` + window.fcSettings = { + token: "${pluginOptions.token}", + host: "${freshchatHost}" + }; + + function addFreshchatScript() { + if (window.fcWidgetAdded) return; + var fcScript = document.createElement('script'); + fcScript.setAttribute('src','https://wchat.freshchat.com/js/widget.js'); + fcScript.setAttribute('async', 'async'); + document.body.appendChild(fcScript); + window.fcWidgetAdded = true; + } + + function changeSignupBtn() { + var signupBtn = document.getElementById('signup-btn'); + if (!signupBtn) { + setTimeout(changeSignupBtn, 1000); + return; + } + document.getElementById('signup-btn').textContent = 'Go to Dashboard'; + document.getElementById('signup-btn').href = 'https://app.testsigma.com'; + } + + // If session details is not received from app within 15 seconds, create new chat. + setTimeout(addFreshchatScript, 15000); + + window.addEventListener('message', function( event) { + if (event.origin === 'https://app.testsigma.com' || + event.origin === 'https://staging.testsigma.com' || + event.origin === 'https://prestaging.testsigma.com' || + event.origin === 'https://devtesting.testsigma.com' || + event.origin === 'http://dev.testsigma.com') { + if (event.data === 'appLoggedOut') { + addFreshchatScript(); + } else { + var session = JSON.parse(event.data); + changeSignupBtn(); + window.fcSettings.externalId = session.user.email; + window.fcSettings.restoreId = session.fcRestoreId; + addFreshchatScript(); + } + } + }); + ` + + setPostBodyComponents([ + , +