11import * as React from 'react' ;
2- import { useAppendHeadScript } from './useAppendHeadScript' ;
2+ import useAppendHeadScript from './useAppendHeadScript' ;
33
44type TUseHotjar = {
55 initHotjar : (
66 hotjarId : string ,
77 hotjarVersion : string ,
8- shouldLog ?: boolean
8+ logCallback ?: ( ) => void
99 ) => boolean ;
1010 identityHotjar : (
1111 userId : string ,
1212 userInfo : string ,
13- shouldLog ?: boolean
13+ logCallback ?: ( ) => void
1414 ) => boolean ;
1515} ;
1616
1717export function useHotjar ( ) : TUseHotjar {
1818 const { appendHeadScript } = useAppendHeadScript ( ) ;
1919
2020 const initHotjar = React . useCallback (
21- ( hotjarId : string , hotjarVersion : string , shouldLog = true ) : boolean => {
21+ ( hotjarId : string , hotjarVersion : string , loggerFunction ) : boolean => {
2222 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 ( {
24- scriptText : hotjarScript ,
25- scriptId : 'hotjar-script' ,
26- } ) ;
23+ const isHotjarAppended = appendHeadScript ( hotjarScript , 'hotjar-script' ) ;
2724
28- if ( shouldLog ) console . info ( `Hotjar ready: ${ isHotjarAppended } ` ) ;
25+ if ( loggerFunction && typeof loggerFunction === 'function' )
26+ loggerFunction ( `Hotjar ready: ${ isHotjarAppended } ` ) ;
2927
3028 return isHotjarAppended ;
3129 } ,
3230 [ appendHeadScript ]
3331 ) ;
3432
3533 const identityHotjar = React . useCallback (
36- ( userId : string , userInfo : string , shouldLog = true ) : boolean => {
34+ ( userId : string , userInfo : string , loggerFunction ) : boolean => {
3735 try {
3836 const hotjarIdentityScript = `var userId="${ userId } " || null;window.hj("identify",userId,${ userInfo } );` ;
39- const isIdentified = appendHeadScript ( {
40- scriptText : hotjarIdentityScript ,
41- scriptId : 'identity-script' ,
42- } ) ;
37+ const isIdentified = appendHeadScript (
38+ hotjarIdentityScript ,
39+ 'identity-script'
40+ ) ;
4341
44- if ( shouldLog ) console . info ( `Hotjar identified: ${ isIdentified } ` ) ;
42+ if ( loggerFunction && typeof loggerFunction === 'function' )
43+ loggerFunction ( `Hotjar identified: ${ isIdentified } ` ) ;
4544
4645 return isIdentified ;
4746 } catch ( error ) {
@@ -54,7 +53,7 @@ export function useHotjar(): TUseHotjar {
5453 ) ;
5554
5655 return React . useMemo ( ( ) => ( { initHotjar, identityHotjar } ) , [
57- identityHotjar ,
5856 initHotjar ,
57+ identityHotjar ,
5958 ] ) ;
6059}
0 commit comments