|
1 | 1 | import * as React from 'react'; |
2 | | -import useAppendHeadScript from './useAppendHeadScript'; |
3 | | - |
4 | | -type TUseHotjar = { |
5 | | - initHotjar: ( |
6 | | - hotjarId: string, |
7 | | - hotjarVersion: string, |
8 | | - logCallback?: () => void |
9 | | - ) => boolean; |
10 | | - identifyHotjar: ( |
11 | | - userId: string, |
12 | | - userInfo: string, |
13 | | - logCallback?: () => void |
14 | | - ) => boolean; |
| 2 | + |
| 3 | +type TScriptLoader = { |
| 4 | + onCreate?: (() => null) | undefined; |
| 5 | + onLoad?: (() => null) | undefined; |
| 6 | + onError?: ((e: any) => never) | undefined; |
| 7 | + delayMs?: number | undefined; |
| 8 | + htmlPart?: string | undefined; |
| 9 | + src: string; |
| 10 | + otherProps?: Record<string, unknown> | undefined; |
| 11 | +}; |
| 12 | + |
| 13 | +type TAppendScript = { |
| 14 | + id: string; |
| 15 | + scriptText: string; |
| 16 | + optionalCallback?: (() => null) | undefined; |
| 17 | + htmlPart: string; |
| 18 | + otherProps?: Record<string, unknown> | undefined; |
| 19 | +}; |
| 20 | + |
| 21 | +type TUseScript = { |
| 22 | + ScriptLoader: { |
| 23 | + ({ |
| 24 | + onCreate, |
| 25 | + onLoad, |
| 26 | + onError, |
| 27 | + delayMs, |
| 28 | + htmlPart, |
| 29 | + src, |
| 30 | + ...otherProps |
| 31 | + }: TScriptLoader): null; |
| 32 | + }; |
| 33 | + appendScript: { |
| 34 | + ({ |
| 35 | + id, |
| 36 | + scriptText, |
| 37 | + optionalCallback, |
| 38 | + htmlPart, |
| 39 | + otherProps, |
| 40 | + }: TAppendScript): boolean; |
| 41 | + }; |
| 42 | +}; |
| 43 | + |
| 44 | +const handleScriptAttributes = ( |
| 45 | + script: HTMLScriptElement, |
| 46 | + otherProps: Record<string, unknown> |
| 47 | +) => { |
| 48 | + for (const [attr, value] of Object.entries(otherProps)) { |
| 49 | + script.setAttribute(attr, value as string); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +const ScriptLoader = ({ |
| 54 | + onCreate = () => null, |
| 55 | + onLoad = () => null, |
| 56 | + onError = (e) => { |
| 57 | + throw new URIError(`The script ${e.target.src} is not accessible`); |
| 58 | + }, |
| 59 | + delayMs = 0, |
| 60 | + htmlPart = 'head', |
| 61 | + src, |
| 62 | + ...otherProps |
| 63 | +}: TScriptLoader): null => { |
| 64 | + const appendScript = React.useCallback(() => { |
| 65 | + const script = global.document.createElement('script'); |
| 66 | + |
| 67 | + script.src = src; |
| 68 | + |
| 69 | + if (otherProps) { |
| 70 | + handleScriptAttributes(script, otherProps as Record<string, unknown>); |
| 71 | + } |
| 72 | + |
| 73 | + script.onload = onLoad; |
| 74 | + script.onerror = onError; |
| 75 | + |
| 76 | + global.document[htmlPart].appendChild(script); |
| 77 | + |
| 78 | + onCreate(); |
| 79 | + }, [onCreate, onError, onLoad, otherProps, src, htmlPart]); |
| 80 | + |
| 81 | + React.useEffect(() => { |
| 82 | + const timeout = setTimeout(() => appendScript(), delayMs); |
| 83 | + |
| 84 | + return () => { |
| 85 | + clearTimeout(timeout); |
| 86 | + }; |
| 87 | + }, [appendScript, delayMs]); |
| 88 | + |
| 89 | + return null; |
| 90 | +}; |
| 91 | + |
| 92 | +const appendScript = ({ |
| 93 | + id, |
| 94 | + scriptText, |
| 95 | + optionalCallback = () => null, |
| 96 | + htmlPart = 'head', |
| 97 | + otherProps = {}, |
| 98 | +}: TAppendScript): boolean => { |
| 99 | + try { |
| 100 | + const existentScript = global.document.getElementById( |
| 101 | + id |
| 102 | + ) as HTMLScriptElement; |
| 103 | + const script = existentScript || global.document.createElement('script'); |
| 104 | + |
| 105 | + script.id = id; |
| 106 | + script.innerText = scriptText.toString(); |
| 107 | + |
| 108 | + handleScriptAttributes(script, otherProps); |
| 109 | + |
| 110 | + global.document[htmlPart].appendChild(script); |
| 111 | + |
| 112 | + optionalCallback(); |
| 113 | + |
| 114 | + return true; |
| 115 | + } catch (error) { |
| 116 | + console.error('Must be a string!', error); |
| 117 | + |
| 118 | + return false; |
| 119 | + } |
15 | 120 | }; |
16 | 121 |
|
17 | | -export function useHotjar(): TUseHotjar { |
18 | | - const { appendHeadScript } = useAppendHeadScript(); |
19 | | - |
20 | | - const initHotjar = React.useCallback( |
21 | | - (hotjarId: string, hotjarVersion: string, loggerFunction): boolean => { |
22 | | - const hotjarScript = `(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=');`; |
23 | | - const isHotjarAppended = appendHeadScript(hotjarScript, 'hotjar-script'); |
24 | | - |
25 | | - if (loggerFunction && typeof loggerFunction === 'function') |
26 | | - loggerFunction(`Hotjar ready: ${isHotjarAppended}`); |
27 | | - |
28 | | - return isHotjarAppended; |
29 | | - }, |
30 | | - [appendHeadScript] |
31 | | - ); |
32 | | - |
33 | | - const identifyHotjar = React.useCallback( |
34 | | - (userId: string, userInfo: string, loggerFunction): boolean => { |
35 | | - try { |
36 | | - const hotjarIdentifyScript = `var userId="${userId}" || null;window.hj("identify",userId,${userInfo});`; |
37 | | - const isIdentified = appendHeadScript( |
38 | | - hotjarIdentifyScript, |
39 | | - 'identify-script' |
40 | | - ); |
41 | | - |
42 | | - if (loggerFunction && typeof loggerFunction === 'function') |
43 | | - loggerFunction(`Hotjar identified: ${isIdentified}`); |
44 | | - |
45 | | - return isIdentified; |
46 | | - } catch (error) { |
47 | | - console.error('Hotjar error:', error); |
48 | | - |
49 | | - return false; |
50 | | - } |
51 | | - }, |
52 | | - [appendHeadScript] |
53 | | - ); |
54 | | - |
55 | | - return React.useMemo(() => ({ initHotjar, identifyHotjar }), [ |
56 | | - initHotjar, |
57 | | - identifyHotjar, |
58 | | - ]); |
| 122 | +export function useScript(): TUseScript { |
| 123 | + return React.useMemo(() => ({ ScriptLoader, appendScript }), []); |
59 | 124 | } |
0 commit comments