Skip to content

Commit b79d1af

Browse files
[3.3.2][TE-6165]: Use same Freshchat session in app and website (#19)
* [3.3.2][TE-6165]: Use same Freshchat session in app and website * [3.3.2][TE-6165]: Update package-lock.json
1 parent b646fad commit b79d1af

7 files changed

Lines changed: 87 additions & 23 deletions

File tree

gatsby-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ module.exports = {
100100
},
101101
},
102102
{
103-
resolve: `gatsby-plugin-freshchat`,
103+
resolve: `gatsby-plugin-ts-freshchat`,
104104
options: {
105105
token: process.env.FRESHCHAT_TOKEN,
106106
host: "https://wchat.freshchat.com",
107+
appEmbedUrl: "https://prestaging.testsigma.com/ui/dashboard",
107108
},
108109
},
109110
{

package-lock.json

Lines changed: 3 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dotenv": "^10.0.0",
2222
"gatsby": "^3.13.0",
2323
"gatsby-plugin-algolia": "^0.22.2",
24-
"gatsby-plugin-freshchat": "^1.0.0-rc1",
24+
"gatsby-plugin-ts-freshchat": "file:plugins/gatsby-plugin-ts-freshchat",
2525
"gatsby-plugin-google-analytics": "^4.14.0",
2626
"gatsby-plugin-google-gtag": "^4.4.0",
2727
"gatsby-plugin-image": "^1.13.0",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const React = require("react")
2+
3+
module.exports.onRenderBody = (
4+
{setPostBodyComponents},
5+
pluginOptions
6+
) => {
7+
if (typeof pluginOptions.token === `undefined`) {
8+
return null
9+
}
10+
11+
const freshchatHost = pluginOptions.host ? pluginOptions.host : `https://wchat.freshchat.com`
12+
13+
const appEmbedUrl =
14+
pluginOptions.appEmbedUrl ? pluginOptions.appEmbedUrl : `https://app.testsigma.com/ui/dashboard`
15+
16+
const renderHTML = () => `
17+
window.fcSettings = {
18+
token: "${pluginOptions.token}",
19+
host: "${freshchatHost}"
20+
};
21+
22+
function addFreshchatScript() {
23+
if (window.fcWidgetAdded) return;
24+
var fcScript = document.createElement('script');
25+
fcScript.setAttribute('src','https://wchat.freshchat.com/js/widget.js');
26+
fcScript.setAttribute('async', 'async');
27+
document.body.appendChild(fcScript);
28+
window.fcWidgetAdded = true;
29+
}
30+
31+
function changeSignupBtn() {
32+
var signupBtn = document.getElementById('signup-btn');
33+
if (!signupBtn) {
34+
setTimeout(changeSignupBtn, 1000);
35+
return;
36+
}
37+
document.getElementById('signup-btn').textContent = 'Go to Dashboard';
38+
document.getElementById('signup-btn').href = 'https://app.testsigma.com';
39+
}
40+
41+
// If session details is not received from app within 15 seconds, create new chat.
42+
setTimeout(addFreshchatScript, 15000);
43+
44+
window.addEventListener('message', function( event) {
45+
if (event.origin === 'https://app.testsigma.com' ||
46+
event.origin === 'https://staging.testsigma.com' ||
47+
event.origin === 'https://prestaging.testsigma.com' ||
48+
event.origin === 'https://devtesting.testsigma.com' ||
49+
event.origin === 'http://dev.testsigma.com') {
50+
if (event.data === 'appLoggedOut') {
51+
addFreshchatScript();
52+
} else {
53+
var session = JSON.parse(event.data);
54+
changeSignupBtn();
55+
window.fcSettings.externalId = session.user.email;
56+
window.fcSettings.restoreId = session.fcRestoreId;
57+
addFreshchatScript();
58+
}
59+
}
60+
});
61+
`
62+
63+
setPostBodyComponents([
64+
<iframe key={`gatsby-plugin-ts-freshchat-iframe`}
65+
src={`${appEmbedUrl}`}
66+
height={0} width={0}
67+
style={{display: "none"}}></iframe>,
68+
<script
69+
key={`gatsby-plugin-ts-freshchat-render`}
70+
dangerouslySetInnerHTML={{__html: renderHTML()}}
71+
/>
72+
])
73+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// noop
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "gatsby-plugin-ts-freshchat",
3+
"version": "1.0.0",
4+
"description": "Gatsby Plugin to add Freshchat to Testsigma sites",
5+
"main": "index.js"
6+
}

src/templates/page.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default ({ data, pageContext }) => {
8484
</nav>
8585
<div className="relative flex items-center justify-between w-2/5 pl-9 pr-9">
8686
<SearchInputBox></SearchInputBox>
87-
<a className="border border-green-600 font-bold px-4 py-1.5 text-green-600 rounded" href='https://testsigma.com/signup'>Get started for free</a>
87+
<a id={'signup-btn'} target={'_blank'} className="border border-green-600 font-bold px-4 py-1.5 text-green-600 rounded" href='https://testsigma.com/signup'>Get started for free</a>
8888
</div>
8989
</nav>
9090
</div>

0 commit comments

Comments
 (0)