├── .tool-versions ├── .npmrc ├── .versionrc.json ├── CHANGELOG.md ├── src └── react-hotjar.js ├── index.js ├── package.json ├── .github └── workflows │ └── npm-publish.yml ├── LICENSE ├── .gitignore ├── index.d.ts └── README.md /.tool-versions: -------------------------------------------------------------------------------- 1 | nodejs 18.4.0 2 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | @abdalla:registry=https://npm.pkg.github.com/ -------------------------------------------------------------------------------- /.versionrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "types": [ 3 | { 4 | "type": "feat", 5 | "section": "Features" 6 | }, 7 | { 8 | "type": "fix", 9 | "section": "Bug Fixes" 10 | }, 11 | { 12 | "type": "chore", 13 | "hidden": true 14 | }, 15 | { 16 | "type": "docs", 17 | "section": "Documentation" 18 | }, 19 | { 20 | "type": "style", 21 | "hidden": true 22 | }, 23 | { 24 | "type": "refactor", 25 | "section": "Refactoring" 26 | }, 27 | { 28 | "type": "test", 29 | "hidden": true 30 | } 31 | ], 32 | "commitAll": true, 33 | "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" 34 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ### [6.1.3](https://github.com/abdalla/react-hotjar/compare/v6.1.2...v6.1.3) (2023-04-12) 6 | 7 | 8 | ### Documentation 9 | 10 | * **changelog:** added changelog file ([44d256b](https://github.com/abdalla/react-hotjar/commit/44d256bd2c53f6257ad0454b561b306030ab59c5)) 11 | 12 | ### [6.1.0](https://github.com/abdalla/react-hotjar/compare/v6.0.0...v6.1.0) (2023-04-11) 13 | 14 | ### Added 15 | - N/A 16 | ### Changed 17 | - Code improvements due to ES6 18 | -------------------------------------------------------------------------------- /src/react-hotjar.js: -------------------------------------------------------------------------------- 1 | module.exports = function({id, sv, debug = false, nonce = null}) { 2 | (function(h, o, t, j, a, r) { 3 | h.hj = 4 | h.hj || 5 | function() { 6 | (h.hj.q = h.hj.q || []).push(arguments); 7 | }; 8 | h._hjSettings = { hjid: id, hjsv: sv, hjDebug: debug }; 9 | h._scriptPath = t + h._hjSettings.hjid + j + h._hjSettings.hjsv; 10 | if(!document.querySelector( 11 | 'script[src*="' + h._scriptPath + '"]' 12 | )){ 13 | a = o.getElementsByTagName('head')[0]; 14 | r = o.createElement('script'); 15 | if (nonce) r.setAttribute('nonce', nonce); 16 | r.async = 1; 17 | r.src = h._scriptPath; 18 | a.appendChild(r); 19 | } 20 | })(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv='); 21 | }; 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const hotjarLib = require('./src/react-hotjar'); 2 | 3 | const hj = (...params) => { 4 | if (!window.hj) { 5 | throw new Error('Hotjar is not initialized'); 6 | } 7 | window.hj(...params); 8 | }; 9 | 10 | module.exports = { 11 | hotjar: { 12 | initialize: function initialize({id, sv, debug, nonce}) { 13 | hotjarLib({id, sv, debug, nonce}); 14 | }, 15 | initialized: function initialized() { 16 | return typeof window !== 'undefined' && typeof window?.hj === 'function'; 17 | }, 18 | identify: function identify(userId, properties) { 19 | hj('identify', userId, properties); 20 | }, 21 | event: function event(event) { 22 | hj('event', event); 23 | }, 24 | stateChange: function stateChange(relativePath) { 25 | hj('stateChange', relativePath); 26 | } 27 | }, 28 | }; 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-hotjar", 3 | "version": "6.3.0", 4 | "description": "Small component to implement Hotjar into your react application", 5 | "main": "index.js", 6 | "scripts": { 7 | "release": "standard-version", 8 | "commit": "git-cz" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/abdalla/react-hotjar.git" 13 | }, 14 | "keywords": [ 15 | "HotJar", 16 | "React" 17 | ], 18 | "author": "Carlos Abdalla", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/abdalla/react-hotjar/issues" 22 | }, 23 | "config": { 24 | "commitizen": { 25 | "path": "cz-conventional-changelog" 26 | } 27 | }, 28 | "homepage": "https://github.com/abdalla/react-hotjar#readme", 29 | "devDependencies": { 30 | "commitizen": "^4.3.0", 31 | "cz-conventional-changelog": "^3.3.0", 32 | "eslint": "8.25.0", 33 | "standard-version": "^9.5.0" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.github/workflows/npm-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created 2 | # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages 3 | 4 | name: Node.js Package 5 | 6 | on: 7 | release: 8 | types: [created] 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | - uses: actions/setup-node@v3 16 | with: 17 | node-version: 16 18 | - run: npm ci 19 | - run: npm test 20 | 21 | publish-npm: 22 | needs: build 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: actions/checkout@v3 26 | - uses: actions/setup-node@v3 27 | with: 28 | node-version: 16 29 | registry-url: https://registry.npmjs.org/ 30 | - run: npm ci 31 | - run: npm publish 32 | env: 33 | NODE_AUTH_TOKEN: ${{secrets.npm_token}} 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Carlos Abdalla 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | .idea 61 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export module hotjar { 2 | /** 3 | * Initialize Hotjar tracking. 4 | * @param id This is the ID which tells Hotjar which site settings it should load and where it should save the data collected. 5 | * @param sv The version of the Tracking Code you are using. This is only needed if Hotjar ever updates the Tracking Code and needs to discontinue older ones. Knowing which version your site includes allows hotjar team to contact you and inform you accordingly. 6 | * @param nonce This is Content Security Policy nonce value. 7 | * @param debug [debug=false] Used to enable debug mode 8 | */ 9 | export function initialize({id, sv, debug, nonce}: { id: number, sv: number, debug?: boolean, nonce?: string}): void; 10 | 11 | /** 12 | * Check if Hotjar has been initialized 13 | */ 14 | export function initialized(): boolean; 15 | 16 | /** 17 | * Identify user 18 | * @param userId Unique ID of a user 19 | * @param properties Additional properties describing your user 20 | */ 21 | export function identify( 22 | userId: string | null, 23 | properties: Record 24 | ): void; 25 | 26 | /** 27 | * Add an event to the current session 28 | * @param event Event to add to the session 29 | */ 30 | export function event( 31 | event: string 32 | ): void; 33 | 34 | /** 35 | * Update the state of the SPA 36 | * @param relativePath Current page URL (such as `product/red-trainer`) 37 | */ 38 | export function stateChange( 39 | relativePath: string, 40 | ): void; 41 | } 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-hotjar 2 | Small component to implement Hotjar into your react application 3 | 4 | # Installation 5 | ```bash 6 | npm install react-hotjar --save 7 | ``` 8 | 9 | # Use / Example 10 | ```javascript 11 | import { hotjar } from 'react-hotjar'; 12 | 13 | hotjar.initialize(options); 14 | 15 | // Identify the user 16 | hotjar.identify('USER_ID', { userProperty: 'value' }); 17 | 18 | // Add an event 19 | hotjar.event('button-click'); 20 | 21 | // Update SPA state 22 | hotjar.stateChange('/my/page'); 23 | 24 | // Check if Hotjar has been initialized before calling its methods 25 | if (hotjar.initialized()) { 26 | hotjar.identify('USER_ID', { userProperty: 'value' }); 27 | } 28 | ``` 29 | `Options` is an object with the following properties: 30 | 31 | - `id`: Stands for 'Hotjar ID' - Your site's ID. This is the ID which tells Hotjar which site settings it should load and where it should save the data collected. 32 | 33 | - `sv`: Stands for 'Hotjar Snippet Version' - The version of the Tracking Code you are using. This is only needed if Hotjar ever updates the Tracking Code and needs to discontinue older ones. Knowing which version your site includes allows hotjar team to contact you and inform you accordingly. 34 | 35 | - `debug`: Stands for 'Debug' - This is a boolean value that tells Hotjar whether to enable the debug mode for the Tracking Code. When set to true, the debug mode will send data to Hotjar regardless of any privacy settings. This is useful when you want to test the Tracking Code and see if it's working as expected. Optional. 36 | 37 | - `nonce`: Stands for 'Nonce' - This is a string value that allows you to control the nonce attribute of the Hotjar script tag. This is useful when you want to implement a Content Security Policy (CSP) on your site. Optional. 38 | 39 | You can learn more from [Understanding the Tracking Code](https://docs.hotjar.com/v1.0/docs/understanding-the-tracking-code) of Hotjar docs 40 | --------------------------------------------------------------------------------