-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba023d5
commit 0612234
Showing
7 changed files
with
262 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
interface IWindowHotjarEmbedded extends Window { | ||
hj?: (method: string, ...data: unknown[]) => void; | ||
} | ||
|
||
declare const window: IWindowHotjarEmbedded; | ||
|
||
export type TUserInfo = Record< | ||
string | number, | ||
string | number | Date | boolean | ||
>; | ||
|
||
export interface IUseHotjar { | ||
readyState: boolean; | ||
initHotjar: ( | ||
hotjarId: number, | ||
hotjarVersion: number, | ||
logCallback?: (...data: unknown[]) => void | ||
) => boolean; | ||
identifyHotjar: ( | ||
userId: string, | ||
userInfo: TUserInfo, | ||
logCallback?: (...data: unknown[]) => void | ||
) => boolean; | ||
stateChange: ( | ||
relativePath: string, | ||
logCallback?: ((...data: unknown[]) => void) | undefined | ||
) => boolean; | ||
} | ||
|
||
export const appendHeadScript = ( | ||
scriptText: string, | ||
scriptId: string | ||
): boolean => { | ||
try { | ||
const existentScript = document.getElementById( | ||
scriptId | ||
) as HTMLScriptElement; | ||
const script = existentScript || document.createElement('script'); | ||
script.id = scriptId; | ||
script.innerText = scriptText; | ||
script.crossOrigin = 'anonymous'; | ||
|
||
document.head.appendChild(script); | ||
|
||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export function hotjarInitScript( | ||
hotjarId: number, | ||
hotjarVersion: number | ||
): boolean { | ||
const hotjarScriptCode = `(function(h,o,t,j,a,r){h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};h._hjSettings={hjid:${hotjarId},hjsv:${hotjarVersion}};a=o.getElementsByTagName('head')[0];r=o.createElement('script');r.async=1;r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;a.appendChild(r);})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');`; | ||
const isAppended = appendHeadScript(hotjarScriptCode, 'hotjar-init-script'); | ||
|
||
if (isAppended && window && window.hj) { | ||
return true; | ||
} | ||
|
||
throw Error('Hotjar initialization failed!'); | ||
} | ||
|
||
export function hotjarStateChangeScript(relativePath: string): void { | ||
if (window && window.hj) { | ||
return window.hj('stateChange', relativePath); | ||
} | ||
|
||
throw Error('Hotjar is not available! Is Hotjar initialized?'); | ||
} | ||
|
||
export function hotjarIdentifyScript( | ||
userId: string | null, | ||
userInfo: TUserInfo | ||
): void { | ||
if (window && window.hj) { | ||
return window.hj('identify', userId, userInfo); | ||
} | ||
|
||
throw Error('Hotjar is not available! Is Hotjar initialized?'); | ||
} | ||
|
||
export function checkReadyState(): boolean { | ||
if (window && window.hj) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as React from 'react'; | ||
import { | ||
checkReadyState, | ||
hotjarIdentifyScript, | ||
hotjarInitScript, | ||
hotjarStateChangeScript, | ||
IUseHotjar, | ||
TUserInfo, | ||
} from './dependencies'; | ||
|
||
export default function useHotjar(): IUseHotjar { | ||
const isReadyState = checkReadyState(); | ||
const [readyState, setReadyState] = React.useState( | ||
React.useMemo(() => isReadyState, [isReadyState]) | ||
); | ||
|
||
const initHotjar = React.useCallback( | ||
( | ||
hotjarId: number, | ||
hotjarVersion: number, | ||
logCallback?: (...data: unknown[]) => void | ||
): boolean => { | ||
try { | ||
hotjarInitScript(hotjarId, hotjarVersion); | ||
|
||
setReadyState(true); | ||
|
||
if (logCallback && typeof logCallback === 'function') | ||
logCallback(`Hotjar ready: true`); | ||
|
||
return true; | ||
} catch (error) { | ||
console.error('Hotjar error:', error); | ||
|
||
return false; | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
const identifyHotjar = React.useCallback( | ||
( | ||
userId: string | null, | ||
userInfo: TUserInfo, | ||
logCallback?: (...data: unknown[]) => void | ||
): boolean => { | ||
try { | ||
hotjarIdentifyScript(userId, userInfo); | ||
|
||
if (logCallback && typeof logCallback === 'function') | ||
logCallback(`Hotjar identified`); | ||
|
||
return true; | ||
} catch (error) { | ||
console.error('Hotjar error:', error); | ||
|
||
return false; | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
const stateChange = React.useCallback( | ||
(relativePath: string, logCallback?: (...data: unknown[]) => void) => { | ||
try { | ||
hotjarStateChangeScript(relativePath); | ||
|
||
if (logCallback && typeof logCallback === 'function') | ||
logCallback(`Hotjar stateChanged`); | ||
|
||
return true; | ||
} catch (error) { | ||
console.error('Hotjar error:', error); | ||
|
||
return false; | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
return React.useMemo( | ||
() => ({ readyState, stateChange, initHotjar, identifyHotjar }), | ||
[readyState, stateChange, initHotjar, identifyHotjar] | ||
); | ||
} |
Oops, something went wrong.