-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# @mbinjamil/matomo-client | ||
|
||
## 0.3.0 | ||
|
||
### Minor Changes | ||
|
||
- Add docs | ||
|
||
## 0.2.0 | ||
|
||
### Minor Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,116 @@ | ||
@mbinjamil/matomo-client | ||
# Matomo client | ||
|
||
## Installation | ||
A framework-agnostic JavaScript client for [Matomo analytics](https://matomo.org/). | ||
|
||
### Installation | ||
|
||
``` | ||
npm install @mbinjamil/matomo-client | ||
``` | ||
|
||
## Features | ||
## Motivation | ||
|
||
The standard installation flow for Matomo is to add a script tag in your HTML, which automatically records a page view when loaded. This is not suitable for single-page applications, like React, Next.js and many others, where client-side routing takes place. Other Matomo JS libraries are outdated and archived. | ||
|
||
Heavily inspired by [fathom-client](https://github.com/derrickreimer/fathom-client), this library provides a simple interface to orchestrate Matomo calls. It features: | ||
|
||
|
||
- Injecting and loading Matomo script asynchronously | ||
- **Asynchronous script loading.** A `load` function that asynchronously injects the Matomo `<script>` tag (great for single-page app environments) and is idempotent (multiple calls to it have no side effects). | ||
- **`import`-able tracking functions.** Tracking functions (`trackPageview` and `trackEvent`) can be imported and safely called anywhere (even if the Matomo script has not yet finished loading). | ||
|
||
## Usage | ||
|
||
Load script on mount and call tracking functions anywhere in the app | ||
The basic way to use this library to call `load` function when your app first loads (or mounts) and call any tracking functions wherever you want. Ideally you would call `trackPageView` whenever your app changes routes. | ||
|
||
Below I've described usage for two popular choices, but you can use it with any frontend library or framework by following the basic principles. | ||
|
||
### React Router (or Remix) | ||
|
||
Create a new component `<Matomo>` | ||
|
||
```jsx | ||
import { useLocation } from "react-router-dom"; | ||
import { load, trackPageView } from "@mbinjamil/matomo-client"; | ||
import { useEffect } from "react"; | ||
|
||
export default function Matomo() { | ||
const location = useLocation(); | ||
|
||
// Load script on mount | ||
useEffect(() => { | ||
load("https://mbinjamildev.matomo.cloud/", 1); | ||
}, []); | ||
|
||
// Track page view on route change | ||
useEffect(() => { | ||
trackPageView(); | ||
}, [location.pathname, location.search]); | ||
|
||
return null; | ||
} | ||
``` | ||
|
||
Then, add the component to the root route or layout | ||
|
||
```jsx | ||
import { NavLink, Outlet, useLocation } from "react-router-dom"; | ||
import Matomo from "./Matomo"; | ||
|
||
export default function Root() { | ||
return ( | ||
<div> | ||
<Matomo /> | ||
<Outlet /> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
### Next.js | ||
|
||
Create a client component `<Matomo>` | ||
|
||
```jsx | ||
"use client" | ||
import { load, trackPageView } from "@mbinjamil/matomo-client"; | ||
import { useEffect } from "react"; | ||
import { usePathname, useSearchParams } from "next/navigation"; | ||
|
||
export default function Matomo() { | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
|
||
// Load script on mount | ||
useEffect(() => { | ||
load("https://mbinjamildev.matomo.cloud/", 1); | ||
}, []); | ||
|
||
// Track page view on route change | ||
useEffect(() => { | ||
trackPageView(); | ||
}, [pathname, searchParams]) | ||
|
||
return null | ||
} | ||
``` | ||
|
||
Then, add the client component to your root `layout.tsx` file: | ||
|
||
```tsx | ||
import Matomo from "./Matomo"; | ||
|
||
export default function RootLayout({ children }: { children: ReactNode }) { | ||
return ( | ||
<html lang="en"> | ||
<head></head> | ||
<body> | ||
<Matomo /> | ||
<Page>{children}</Page> | ||
</body> | ||
</html> | ||
); | ||
} | ||
``` | ||
|
||
### [API Reference](docs/README.md) | ||
|
||
### [API Reference](docs/README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters