├── .github ├── FUNDING.yml └── workflows │ └── build.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── demos └── react-router │ ├── .gitignore │ ├── README.md │ ├── package-lock.json │ ├── package.json │ ├── public │ └── index.html │ └── src │ ├── About.js │ ├── App.js │ ├── Content.js │ ├── Dashboard.js │ ├── Home.js │ └── index.js ├── package-lock.json ├── package.json ├── src └── index.js └── test └── index.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: electerious 2 | custom: ['https://paypal.me/electerious', 'https://www.buymeacoffee.com/electerious'] -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | pull_request: 5 | paths-ignore: 6 | - '**.md' 7 | push: 8 | paths-ignore: 9 | - '**.md' 10 | 11 | jobs: 12 | cancel: 13 | 14 | name: 'Cancel previous runs' 15 | runs-on: ubuntu-latest 16 | timeout-minutes: 2 17 | 18 | steps: 19 | - uses: styfle/cancel-workflow-action@0.5.0 20 | with: 21 | access_token: ${{ github.token }} 22 | 23 | build: 24 | 25 | name: 'Build and test' 26 | runs-on: ${{ matrix.os }} 27 | timeout-minutes: 10 28 | 29 | strategy: 30 | matrix: 31 | node-version: [18.x, 20.x, 22.x] 32 | os: [ubuntu-latest] 33 | 34 | steps: 35 | - uses: actions/checkout@v2 36 | 37 | - name: Use Node.js ${{ matrix.node-version }} 38 | uses: actions/setup-node@v1 39 | with: 40 | node-version: ${{ matrix.node-version }} 41 | 42 | - run: npm i 43 | 44 | - run: npm test 45 | 46 | - run: npm run coveralls 47 | 48 | - name: Parallel Coveralls report 49 | uses: coverallsapp/github-action@master 50 | with: 51 | github-token: ${{ github.token }} 52 | flag-name: run-${{ matrix.test_number }} 53 | parallel: true 54 | 55 | coveralls: 56 | 57 | needs: build 58 | runs-on: ubuntu-latest 59 | 60 | steps: 61 | - name: Finished Coveralls report 62 | uses: coverallsapp/github-action@master 63 | with: 64 | github-token: ${{ github.token }} 65 | parallel-finished: true -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | .nyc_output/ 4 | .env 5 | .DS_Store -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## Unreleased 8 | 9 | ### Changed 10 | 11 | - Requires Node.js 18 or newer 12 | 13 | ## [3.1.0] - 2024-06-13 14 | 15 | ### Changed 16 | 17 | - Support for React 16.8, 17 and 18 18 | 19 | ## [3.0.1] - 2022-01-09 20 | 21 | ### Added 22 | 23 | - Console warning when called without `pathname` 24 | 25 | ### Fixed 26 | 27 | - `ReferenceError: location is not defined` error when called in isomorphic code 28 | 29 | ## [3.0.0] - 2021-02-21 30 | 31 | ### Changed 32 | 33 | - Requires React 17 or newer 34 | 35 | ## [2.0.0] - 2021-01-24 36 | 37 | ### Changed 38 | 39 | - Requires Ackee v3 or newer 40 | 41 | ### Fixed 42 | 43 | - Changing the server URL, domain id or an option should recreate the instance 44 | 45 | ## [1.0.1] - 2020-09-20 46 | 47 | ### Changed 48 | 49 | - Updated dependencies 50 | 51 | ## [1.0.0] - 2020-08-15 52 | 53 | ### Changed 54 | 55 | - Updated [ackee-tracker](https://github.com/electerious/ackee-tracker) to support Ackee v2 56 | - Dropped support for older Ackee versions (< 2.0) 57 | 58 | ## [0.3.0] - 2020-05-26 59 | 60 | ### Fixed 61 | 62 | - Stop updating a record when the component unmounts 63 | 64 | ## [0.2.0] - 2020-03-20 65 | 66 | ### Changed 67 | 68 | - Updated dependencies 69 | - Test with Node.js 10 and 12 70 | 71 | ## [0.1.0] - 2019-11-10 72 | 73 | ### Added 74 | 75 | - Everything -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Tobias Reich 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # use-ackee 2 | 3 | ![Build](https://github.com/electerious/use-ackee/workflows/Build/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/electerious/use-ackee/badge.svg?branch=master)](https://coveralls.io/github/electerious/use-ackee?branch=master) 4 | 5 | Use [Ackee](https://github.com/electerious/Ackee) in React. 6 | 7 | ## Install 8 | 9 | ``` 10 | npm install use-ackee 11 | ``` 12 | 13 | *or* 14 | 15 | ``` 16 | yarn add use-ackee 17 | ``` 18 | 19 | ## Usage 20 | 21 | Import and call `use-ackee` in a component responsible for the routing or inside your routes. Make sure that only one `use-ackee` hook is actively rendered to avoid redundant records. 22 | 23 | ```js 24 | useAckee('/current/path', { 25 | server: 'https://example.com', 26 | domainId: 'hd11f820-68a1-11e6-8047-79c0c2d9bce0' 27 | }, { 28 | detailed: false, 29 | ignoreLocalhost: true, 30 | ignoreOwnVisits: true 31 | }) 32 | ``` 33 | 34 | Ackee will create a new record every time the `pathname` changes. An undefined or empty `pathname` will be skipped. Use `/` for the root instead. 35 | 36 | This hook is a no-op on the server for safe usage during server-side rendering. 37 | 38 | ## API 39 | 40 | ### Parameters 41 | 42 | - `pathname` `{?String}` Current path. 43 | - `environment` `{Object}` An object that contains details about your [Ackee](https://github.com/electerious/Ackee) installation. The `server` property must not end with a slash. 44 | - `opts` `{?Object}` An object of [Ackee options](https://github.com/electerious/ackee-tracker#options). 45 | 46 | ## Examples 47 | 48 | - [React Router](demos/react-router/README.md) -------------------------------------------------------------------------------- /demos/react-router/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | .eslintcache 21 | 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | -------------------------------------------------------------------------------- /demos/react-router/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `yarn build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /demos/react-router/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-router", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "react-scripts start", 7 | "build": "react-scripts build", 8 | "test": "react-scripts test", 9 | "eject": "react-scripts eject" 10 | }, 11 | "browserslist": { 12 | "production": [ 13 | ">0.2%", 14 | "not dead", 15 | "not op_mini all" 16 | ], 17 | "development": [ 18 | "last 1 chrome version", 19 | "last 1 firefox version", 20 | "last 1 safari version" 21 | ] 22 | }, 23 | "dependencies": { 24 | "react": "^17.0.2", 25 | "react-dom": "^17.0.2", 26 | "react-router-dom": "^5.2.0", 27 | "react-scripts": "4.0.1", 28 | "use-ackee": "^3.0.1" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /demos/react-router/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | useAckee with React Router 7 | 8 | 9 |
10 | 11 | -------------------------------------------------------------------------------- /demos/react-router/src/About.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | const About = () => { 4 | return ( 5 |
6 |

About

7 |
8 | ) 9 | } 10 | 11 | export default About -------------------------------------------------------------------------------- /demos/react-router/src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { BrowserRouter as Router } from 'react-router-dom' 3 | 4 | import Content from './Content' 5 | 6 | const App = () => { 7 | return ( 8 | 9 | 10 | 11 | ) 12 | } 13 | 14 | export default App -------------------------------------------------------------------------------- /demos/react-router/src/Content.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { Switch, Route, Link, useLocation } from 'react-router-dom' 3 | 4 | import useAckee from 'use-ackee' 5 | 6 | import Home from './Home' 7 | import About from './About' 8 | import Dashboard from './Dashboard' 9 | 10 | const Content = () => { 11 | const location = useLocation() 12 | 13 | useAckee(location.pathname, { 14 | server: 'https://example.com', 15 | domainId: 'hd11f820-68a1-11e6-8047-79c0c2d9bce0', 16 | }, { 17 | ignoreLocalhost: false, 18 | }) 19 | 20 | return ( 21 |
22 | 33 | 34 |
35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
48 | ) 49 | } 50 | 51 | export default Content -------------------------------------------------------------------------------- /demos/react-router/src/Dashboard.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | const Dashboard = () => { 4 | return ( 5 |
6 |

Dashboard

7 |
8 | ) 9 | } 10 | 11 | export default Dashboard -------------------------------------------------------------------------------- /demos/react-router/src/Home.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | const Home = () => { 4 | return ( 5 |
6 |

Home

7 |
8 | ) 9 | } 10 | 11 | export default Home -------------------------------------------------------------------------------- /demos/react-router/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ReactDOM from 'react-dom' 3 | 4 | import App from './App' 5 | 6 | ReactDOM.render(, document.getElementById('root')) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "use-ackee", 3 | "version": "3.1.0", 4 | "authors": [ 5 | "Tobias Reich " 6 | ], 7 | "description": "Use Ackee in React", 8 | "main": "src/index.js", 9 | "keywords": [ 10 | "react", 11 | "hooks", 12 | "hook", 13 | "tracker", 14 | "analytics", 15 | "tracking", 16 | "ackee" 17 | ], 18 | "license": "MIT", 19 | "homepage": "https://github.com/electerious/use-ackee", 20 | "repository": { 21 | "type": "git", 22 | "url": "https://github.com/electerious/use-ackee.git" 23 | }, 24 | "files": [ 25 | "src" 26 | ], 27 | "scripts": { 28 | "coveralls": "nyc report --reporter=lcov", 29 | "test": "npm run lint && nyc node_modules/mocha/bin/_mocha", 30 | "lint": "eslint \"{src,test}/**/*.js\"" 31 | }, 32 | "dependencies": { 33 | "ackee-tracker": "^5.1.0" 34 | }, 35 | "devDependencies": { 36 | "@electerious/eslint-config": "^4.1.0", 37 | "chai": "^4.3.0", 38 | "coveralls": "^3.1.1", 39 | "mocha": "^9.1.3", 40 | "nyc": "^15.1.0", 41 | "react": "^18.3.1" 42 | }, 43 | "peerDependencies": { 44 | "react": "^16.8.0 || ^17.0.0 || ^18.0.0" 45 | }, 46 | "engines": { 47 | "node": ">= 18" 48 | }, 49 | "eslintConfig": { 50 | "root": true, 51 | "extends": "@electerious/eslint-config" 52 | }, 53 | "eslintIgnore": [ 54 | "demos" 55 | ] 56 | } 57 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { useMemo, useEffect } = require('react') 4 | const ackeeTracker = require('ackee-tracker') 5 | 6 | const isBrowser = typeof window !== 'undefined' 7 | 8 | /** 9 | * Use Ackee in React. 10 | * Creates an instance once and a new record every time the pathname changes. 11 | * Safely no-ops during server-side rendering. 12 | * @param {?String} pathname - Current path. 13 | * @param {Object} environment - Object containing the URL of the Ackee server and the domain id. 14 | * @param {?Object} options - Ackee options. 15 | */ 16 | const useAckee = function(pathname, environment, options = {}) { 17 | const instance = useMemo(() => { 18 | if (isBrowser === false) return 19 | 20 | return ackeeTracker.create(environment.server, options) 21 | }, [ environment.server, options.detailed, options.ignoreLocalhost, options.ignoreOwnVisits ]) 22 | 23 | useEffect(() => { 24 | if (instance == null) { 25 | console.warn('Skipped record creation because useAckee has been called in a non-browser environment') 26 | return 27 | } 28 | 29 | const hasPathname = ( 30 | pathname != null && 31 | pathname !== '' 32 | ) 33 | 34 | if (hasPathname === false) { 35 | console.warn('Skipped record creation because useAckee has been called without pathname') 36 | return 37 | } 38 | 39 | const attributes = ackeeTracker.attributes(options.detailed) 40 | const url = new URL(pathname, location) 41 | 42 | return instance.record(environment.domainId, { 43 | ...attributes, 44 | siteLocation: url.href, 45 | }).stop 46 | }, [ instance, pathname, environment.domainId ]) 47 | } 48 | 49 | module.exports = useAckee 50 | module.exports.useAckee = useAckee -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const assert = require('chai').assert 4 | const index = require('./../src/index.js') 5 | 6 | describe('index', function() { 7 | it('should be a function', function() { 8 | assert.isFunction(index) 9 | }) 10 | }) --------------------------------------------------------------------------------