forked from binary-com/deriv-com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.js
185 lines (166 loc) · 6.34 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React from 'react'
import { Pushwoosh } from 'web-push-notifications'
import { WrapPagesWithLocaleContext } from './src/components/localization'
import { isProduction, isLive, isLocalHost } from './src/common/websocket/config'
import { LocalStore } from './src/common/storage'
import { MediaContextProvider } from './src/themes/media'
import { DerivProvider } from './src/store'
import { checkLiveChatRedirection } from './src/common/live-chat-redirection-checking.js'
import { getClientInformation, getDomain, getLanguage, addScript } from 'common/utility'
import { gtm_test_domain, pushwoosh_app_code } from 'common/constants'
import './static/css/ibm-plex-sans-var.css'
const is_browser = typeof window !== 'undefined'
const checkDomain = () => {
return eval(
decodeURIComponent(
'var%20curhost%20%3D%20window.location.hostname%3B%20var%20t8hvj%20%3D%20%2F%5Cb%28deriv%7Cbinary%7Cbinaryqa%5B0-9%5D%7B2%7D%29%5C.%28com%7Cbot%7Cme%7Cbe%7Capp%7Csx%29%24%7C%5Cb%28localhost%29%2Fgm%3B%20if%20%28t8hvj.test%28curhost%29%20%3D%3D%20false%29%7Balert%28%22Not%20our%20domain%22%29%7D',
),
)
}
const sendTags = (api) => {
const language = LocalStore.get('i18n') || ''
const domain = getDomain()
const { loginid, residence } = getClientInformation(domain) || {
loginid: '',
residence: '',
}
api.getTags()
.then((result) => {
if (
!result.result['Login ID'] ||
!result.result['Site Language'] ||
!result.result.Residence
) {
return api.setTags({
'Login ID': loginid,
'Site Language': language.toLowerCase(),
Residence: residence,
})
}
return null
})
.catch((e) => {
// eslint-disable-next-line no-console
console.error(e)
return null
})
}
const pushwooshInit = (push_woosh) => {
push_woosh.push([
'init',
{
logLevel: 'error', // or info or debug
applicationCode: pushwoosh_app_code,
safariWebsitePushID: 'web.com.deriv',
defaultNotificationTitle: 'Deriv.com',
defaultNotificationImage: 'https://deriv.com/favicons/favicon-192x192.png',
},
])
push_woosh.push([
'onReady',
function (api) {
try {
push_woosh.isSubscribed().then((is_subscribed) => {
if (!is_subscribed) {
push_woosh.subscribe()
}
})
// eslint-disable-next-line no-empty
} catch {}
sendTags(api)
},
])
}
export const wrapRootElement = ({ element }) => {
return (
<DerivProvider>
<MediaContextProvider>{element}</MediaContextProvider>
</DerivProvider>
)
}
export const onInitialClientRender = () => {
if (is_browser) {
// Check for PerformanceLongTaskTiming compatibility before collecting measurement
const tti_script = document.createElement('script')
tti_script.type = 'text/javascript'
tti_script.text = `!(function () {
if ('PerformanceLongTaskTiming' in window) {
var g = (window.__tti = { e: [] });
g.o = new PerformanceObserver(function (l) {
g.e = g.e.concat(l.getEntries());
});
g.o.observe({ entryTypes: ['longtask'] });
}
})();`
document.head.appendChild(tti_script)
// Enable translation
// Check if not production and match ach or ach/
const match_ach = window.location.pathname.match(/^(\/ach\/)|\/ach$/)
if (match_ach) {
// TODO: remove this line when production ready for translation
if (!isProduction()) LocalStore.set('i18n', 'ach')
}
const i18n = LocalStore.get('i18n')
if (!isProduction() && i18n && i18n.match('ach')) {
const jipt = document.createElement('script')
jipt.type = 'text/javascript'
jipt.text = `
var _jipt = []; _jipt.push(['project', 'deriv-com']);
var crowdin = document.createElement("script");
crowdin.setAttribute('src', '//cdn.crowdin.com/jipt/jipt.js');
document.head.appendChild(crowdin);
`
document.head.appendChild(jipt)
}
}
}
export const onClientEntry = () => {
const is_gtm_test_domain = window.location.hostname === gtm_test_domain
const push_woosh = new Pushwoosh()
if (isLive()) {
pushwooshInit(push_woosh)
}
// Add GTM script for test domain
if (!isLocalHost() && is_gtm_test_domain) {
addScript({
src: 'https://www.googletagmanager.com/gtm.js?id=GTM-TNX2ZKH',
id: 'gtm-test-container',
})
addScript({
text: `
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-TNX2ZKH');`,
})
}
addScript({
src: 'https://static.deriv.com/scripts/cookie.js',
async: true,
})
checkLiveChatRedirection()
}
export const onRouteUpdate = () => {
checkDomain()
const dataLayer = window.dataLayer
const domain = getDomain()
const client_information = getClientInformation(domain)
const is_logged_in = !!client_information
// wrap inside a timeout to ensure the title has properly been changed
setTimeout(() => {
const eventName = 'page_load'
dataLayer?.push({
event: eventName,
loggedIn: is_logged_in,
language: getLanguage(),
...(is_logged_in && {
visitorId: client_information.loginid,
currency: client_information.currency,
email: client_information.email,
userId: client_information.user_id,
}),
})
}, 1500)
}
export const wrapPageElement = WrapPagesWithLocaleContext