|
18 | 18 | ## Install |
19 | 19 |
|
20 | 20 | ```bash |
21 | | -npm install --save react-use-hotjar |
| 21 | +npm install --save react-use-script |
22 | 22 | ``` |
23 | 23 |
|
24 | 24 | --- |
25 | 25 |
|
26 | 26 | ## Usage |
27 | 27 |
|
28 | | -- Initializing Hotjar (use it at your very `index.jsx`) |
| 28 | +- Use script tags in your **JSX** |
29 | 29 |
|
30 | 30 | ```tsx |
31 | 31 | import * as React from 'react'; |
| 32 | +import { useScript } from 'react-use-script'; |
| 33 | + |
| 34 | +const App = () => { |
| 35 | + const { ScriptLoader } = useScript(); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <ScriptLoader |
| 40 | + id="custom-script" |
| 41 | + src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" |
| 42 | + delayMs={0} |
| 43 | + onCreate={() => console.log('created!')} |
| 44 | + type="text/javascript" |
| 45 | + /> |
| 46 | + </div> |
| 47 | + ); |
| 48 | +}; |
| 49 | +``` |
32 | 50 |
|
33 | | -import { useHotjar } from 'react-use-hotjar'; |
| 51 | +- Append scripts to the document programmatically |
34 | 52 |
|
35 | | -const myCustomLogger = console.info; |
| 53 | +```tsx |
| 54 | +import * as React from 'react'; |
| 55 | +import { useScript } from 'react-use-script'; |
36 | 56 |
|
37 | | -const HotjarReadyApp = () => { |
38 | | - const { initHotjar } = useHotjar(); |
| 57 | +const App = () => { |
| 58 | + const { appendScript } = useScript(); |
39 | 59 |
|
40 | 60 | React.useEffect(() => { |
41 | | - initHotjar(hotjarId, hotjarVersion, myCustomLogger); |
42 | | - }); |
43 | | - |
44 | | - return <App />; |
| 61 | + appendScript({ |
| 62 | + id: 'script-append', |
| 63 | + scriptText: "console.log('my script has been called')", |
| 64 | + optionalCallback: console.log('optional callback'), |
| 65 | + }); |
| 66 | + }, [appendScript]); |
| 67 | + |
| 68 | + return ( |
| 69 | + <div> |
| 70 | + <h1>Script appended to the head programmatically!</h1> |
| 71 | + </div> |
| 72 | + ); |
45 | 73 | }; |
46 | 74 | ``` |
47 | 75 |
|
48 | | -- Identifying Users (use it wherever you get access to user's information) |
| 76 | +--- |
| 77 | + |
| 78 | +## Documentation |
| 79 | + |
| 80 | +`useScript()` returns: |
| 81 | + |
| 82 | +1. <ScriptLoader /> |
| 83 | + |
| 84 | +- Props |
49 | 85 |
|
50 | 86 | ```tsx |
51 | | -const MyCustomComponent = () => { |
52 | | - const { initHotjar } = useHotjar(); |
53 | | - |
54 | | - const handleUserInfo = (userInfo) => { |
55 | | - const { id, ...restUserInfo } = userInfo; |
56 | | - |
57 | | - identifyHotjar( |
58 | | - id, |
59 | | - JSON.stringify({ |
60 | | - restUserInfo, |
61 | | - }) |
62 | | - ); |
63 | | - }; |
| 87 | +type ScriptLoader = { |
| 88 | + onCreate?: (() => null) | undefined; // runs after script tag rendering |
| 89 | + onLoad?: (() => null) | undefined; // runs on script load |
| 90 | + onError?: ((e: any) => never) | undefined; // runs on script error |
| 91 | + delayMs?: number | undefined; // run with delayed start |
| 92 | + htmlPart?: string | undefined; // choose where to append, HEAD or BODY |
| 93 | + src: string; // script file source path |
| 94 | + otherProps?: Record<string, unknown> | undefined; // html script tag properties |
64 | 95 | }; |
65 | 96 | ``` |
66 | 97 |
|
67 | | ---- |
| 98 | +- Default Props |
68 | 99 |
|
69 | | -## Documentation |
70 | | - |
71 | | -`useHotjar()` returns: |
| 100 | +```tsx |
| 101 | +src: undefined; |
| 102 | +onCreate = () => null; |
| 103 | +onLoad = () => null; |
| 104 | +onError = (e) => { |
| 105 | + throw new URIError(`The script ${e.target.src} is not accessible`); |
| 106 | +}; |
| 107 | +delayMs = 0; |
| 108 | +htmlPart = 'head'; |
| 109 | +otherProps: undefined; |
| 110 | +``` |
72 | 111 |
|
73 | | -- initHotjar() |
| 112 | +2. appendScript() |
74 | 113 |
|
75 | | -1. `hotjarId`: Your Hotjar application ID ex.: 1933331 |
76 | | -2. `hotjarVersion`: Hotjar's current version ex.: 6 |
77 | | -3. `logCallback`: Optional callback for logging wether Hotjar is ready or not |
| 114 | +- Props |
78 | 115 |
|
79 | 116 | ```tsx |
80 | | -initHotjar: ( |
81 | | - hotjarId: string, |
82 | | - hotjarVersion: string, |
83 | | - logCallback?: () => void |
84 | | -) => boolean; |
| 117 | +type AppendScript = { |
| 118 | + id: string; // script id |
| 119 | + scriptText: string; // script code as string |
| 120 | + optionalCallback?: (() => null) | undefined; // optional callback function after running |
| 121 | + htmlPart: string; // choose where to append, HEAD or BODY |
| 122 | + otherProps?: Record<string, unknown> | undefined; // html script tag properties |
| 123 | +}; |
85 | 124 | ``` |
86 | 125 |
|
87 | | -- identifyHotjar() |
88 | | - |
89 | | -1. `userId`: Unique user's identification as string |
90 | | -2. `userInfo`: Stringfied user info of key-value pairs (note this must not be so long and deep according to [docs](https://help.hotjar.com/hc/en-us/articles/360033640653-Identify-API-Reference)) |
91 | | -3. `logCallback`: Optional callback for logging wether Hotjar identified user or not |
| 126 | +- Default Props |
92 | 127 |
|
93 | 128 | ```tsx |
94 | | -identifyHotjar: (userId: string, userInfo: string, logCallback?: () => void) => |
95 | | - boolean; |
| 129 | +id: undefined; |
| 130 | +scriptText: undefined; |
| 131 | +optionalCallback = () => null; |
| 132 | +htmlPart = 'head'; |
| 133 | +otherProps = {}; |
96 | 134 | ``` |
97 | 135 |
|
98 | 136 | --- |
99 | 137 |
|
100 | 138 | ## License |
101 | 139 |
|
102 | | -react-use-hotjar is [MIT licensed](./LICENSE). |
| 140 | +react-use-script is [MIT licensed](./LICENSE). |
103 | 141 |
|
104 | 142 | --- |
105 | 143 |
|
|
0 commit comments