Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Support for Debug Mode #6

Merged
merged 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const HotjarReadyApp = () => {
const { initHotjar } = useHotjar();

React.useEffect(() => {
initHotjar(1234567, 6, myCustomLogger);
initHotjar(1234567, 6, false, myCustomLogger);
}, [initHotjar]);

return <App />;
Expand Down Expand Up @@ -89,20 +89,22 @@ const MyCustomComponent = () => {
| key | description | arguments | example |
| -------------- | -------------------------- | --------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| readyState | States if Hotjar is ready | N/A | N/A |
| initHotjar | Initialize method | (hotjarId: number, hotjarVersion: number, loggerCallback?: console[method]) | (1933331, 6, console.info) |
| initHotjar | Initialize method | (hotjarId: number, hotjarVersion: number, hotjarDebug?: boolean, loggerCallback?: console[method]) | (1933331, 6, false, console.info) |
| identifyHotjar | User identify API method | (userId: string, userInfo: object, loggerCallback?: console[method]) | ('abcde-12345-12345', {name:"Olli",surname:"Parno",address:"Streets of Tomorrow"}, console.log) |
| stateChange | Relative path state change | (relativePath: string, loggerCallback?: console[method]) | ('route/logged-route/user?registered=true') |

- initHotjar()

1. `hotjarId`: Your Hotjar application ID ex.: 1933331
2. `hotjarVersion`: Hotjar's current version ex.: 6
3. `logCallback`: Optional callback for logging wether Hotjar is ready or not
3. `hotjarDebug`: Optional Debug Mode to see hotjar logs in console ex.: true
4. `logCallback`: Optional callback for logging wether Hotjar is ready or not

```tsx
initHotjar: (
hotjarId: string,
hotjarVersion: string,
hotjarDebug?: boolean,
logCallback?: () => void
) => boolean;
```
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ describe('Tests useHotjar', () => {

const logCallback = console.info;

initHotjar(123, 6, logCallback);
initHotjar(123, 6, false, logCallback);

expect(initHotjarSpy).toHaveBeenCalledWith(123, 6, logCallback);
expect(initHotjarSpy).toHaveBeenCalledWith(123, 6, false, logCallback);
expect(consoleInfoSpy).toHaveBeenCalledWith('Hotjar ready: true');
});

Expand Down
6 changes: 4 additions & 2 deletions src/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IUseHotjar {
initHotjar: (
hotjarId: number,
hotjarVersion: number,
hotjarDebug?: boolean,
logCallback?: (...data: unknown[]) => void
) => boolean;
identifyHotjar: (
Expand Down Expand Up @@ -50,13 +51,14 @@ export const appendHeadScript = (

export function hotjarInitScript(
hotjarId: number,
hotjarVersion: number
hotjarVersion: number,
hotjardebug: boolean
): boolean {
const hasWindow = typeof window !== 'undefined';

if (!hasWindow) return false;

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 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},hjdebug:${hotjardebug}};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 && hasWindow && window.hj) {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ export default function useHotjar(): IUseHotjar {
(
hotjarId: number,
hotjarVersion: number,
hotjarDebug?: boolean,
olavoparno marked this conversation as resolved.
Show resolved Hide resolved
logCallback?: (...data: unknown[]) => void
): boolean => {
try {
hotjarInitScript(hotjarId, hotjarVersion);
hotjarInitScript(hotjarId, hotjarVersion, !!hotjarDebug);

setReadyState(true);

Expand Down