-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Extract tracker code into its own module #4111
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
Open
dmke
wants to merge
2
commits into
umami-software:master
Choose a base branch
from
dmke:reusable-tracker-package
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+812
−376
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ package-lock.json | |
| /dist | ||
| /generated | ||
| /src/generated | ||
| /packages/tracker/dist | ||
|
|
||
| # misc | ||
| .DS_Store | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| # @umami/tracker | ||
|
|
||
| Umami analytics tracker library for JavaScript/TypeScript projects. | ||
|
|
||
| ## Overview | ||
|
|
||
| This package contains reusable tracking logic for the Umami analytics platform. | ||
|
|
||
| It can be used in any JavaScript or TypeScript project to integrate Umami analytics | ||
| programmatically, i.e. without resorting to an embed code. | ||
|
|
||
| While this does require a separate build tool, you'll likely save a network trip | ||
| and have a higher probability of avoiding ad blockers. Do keep in mind, that | ||
| this dependency needs to be kept up-to-date (matching your Umami instance). | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @umami/tracker | ||
| # or | ||
| pnpm add @umami/tracker | ||
| # or | ||
| yarn add @umami/tracker | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic Setup | ||
|
|
||
| ```typescript | ||
| import { createTracker } from '@umami/tracker'; | ||
|
|
||
| const tracker = createTracker({ | ||
| website: 'your-website-id', | ||
| endpoint: 'https://your-umami-instance.com/api/send', | ||
| }); | ||
|
|
||
| // Track a page view | ||
| tracker.track(); | ||
|
|
||
| // Track a custom event | ||
| tracker.track('button-click'); | ||
|
|
||
| // Track an event with data | ||
| tracker.track('signup', { | ||
| plan: 'premium', | ||
| source: 'homepage', | ||
| }); | ||
| ``` | ||
|
|
||
| ### Configuration Options | ||
|
|
||
| ```typescript | ||
| interface TrackerConfig { | ||
| website: string; // Required: Your website ID | ||
| endpoint?: string; // API endpoint URL | ||
| tag?: string; // Optional tag for categorizing data | ||
| autoTrack?: boolean; // Enable automatic page view tracking (default: true) | ||
| domains?: string[]; // Limit tracking to specific domains | ||
| excludeSearch?: boolean; // Exclude search params from URLs | ||
| excludeHash?: boolean; // Exclude hash from URLs | ||
| doNotTrack?: boolean; // Respect Do Not Track browser setting | ||
| beforeSend?: (type, payload) => payload | null; // Modify or cancel events | ||
| credentials?: RequestCredentials; // Fetch credentials mode (default: 'omit') | ||
| } | ||
| ``` | ||
|
|
||
| ### Advanced Usage | ||
|
|
||
| #### User Identification | ||
|
|
||
| ```typescript | ||
| // Identify a user | ||
| tracker.identify('user-123'); | ||
|
|
||
| // Identify with additional data | ||
| tracker.identify('user-123', { | ||
| email: 'user@example.com', | ||
| plan: 'premium', | ||
| }); | ||
|
|
||
| // Or identify with just data | ||
| tracker.identify({ | ||
| company: 'Acme Inc', | ||
| role: 'admin', | ||
| }); | ||
| ``` | ||
|
|
||
| #### Custom Event Functions | ||
|
|
||
| ```typescript | ||
| // Track with a custom function | ||
| tracker.track((props) => ({ | ||
| ...props, | ||
| name: 'custom-event', | ||
| data: { | ||
| timestamp: Date.now(), | ||
| }, | ||
| })); | ||
| ``` | ||
|
|
||
| #### Manual Initialization | ||
|
|
||
| ```typescript | ||
| const tracker = createTracker({ | ||
| website: 'your-website-id', | ||
| endpoint: 'https://your-umami-instance.com/api/send', | ||
| autoTrack: false, // Disable auto-tracking | ||
| }); | ||
|
|
||
| // Manually initialize when ready | ||
| tracker.init(); | ||
| ``` | ||
|
|
||
| #### Before Send Hook | ||
|
|
||
| ```typescript | ||
| const tracker = createTracker({ | ||
| website: 'your-website-id', | ||
| endpoint: 'https://your-umami-instance.com/api/send', | ||
| beforeSend: (type, payload) => { | ||
| // Modify payload | ||
| payload.custom_field = 'value'; | ||
|
|
||
| // Or cancel the event by returning null | ||
| if (payload.url.includes('admin')) { | ||
| return null; | ||
| } | ||
|
|
||
| return payload; | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## TypeScript Support | ||
|
|
||
| This package is written in TypeScript and includes full type definitions. | ||
|
|
||
| ```typescript | ||
| import type { TrackerConfig, UmamiTracker, EventData } from '@umami/tracker'; | ||
| ``` | ||
|
|
||
| ## Framework Integration | ||
|
|
||
| ### React | ||
|
|
||
| ```typescript | ||
| import { useEffect } from 'react'; | ||
| import { createTracker } from '@umami/tracker'; | ||
|
|
||
| const tracker = createTracker({ | ||
| website: 'your-website-id', | ||
| endpoint: 'https://your-umami-instance.com/api/send', | ||
| }); | ||
|
|
||
| function MyApp() { | ||
| useEffect(() => { | ||
| tracker.track(); // Track page view on mount | ||
| }, []); | ||
|
|
||
| return ( | ||
| <button onClick={() => tracker.track('button-click')}> | ||
| Click me | ||
| </button> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ### Vue | ||
|
|
||
| ```typescript | ||
| import { onMounted } from 'vue'; | ||
| import { createTracker } from '@umami/tracker'; | ||
|
|
||
| const tracker = createTracker({ | ||
| website: 'your-website-id', | ||
| endpoint: 'https://your-umami-instance.com/api/send', | ||
| }); | ||
|
|
||
| export default { | ||
| setup() { | ||
| onMounted(() => { | ||
| tracker.track(); | ||
| }); | ||
|
|
||
| const handleClick = () => { | ||
| tracker.track('button-click'); | ||
| }; | ||
|
|
||
| return { handleClick }; | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## Browser Compatibility | ||
|
|
||
| This package works in all modern browsers that support: | ||
| - ES2020 features | ||
| - `fetch` API | ||
| - `Promise` | ||
| - `URL` API | ||
|
|
||
| ## Zero Dependencies | ||
|
|
||
| This package has zero runtime dependencies, keeping your bundle size small. | ||
|
|
||
| ## License | ||
|
|
||
| MIT | ||
|
|
||
| ## Related | ||
|
|
||
| - [Umami Analytics](https://umami.is) - The main Umami analytics platform | ||
| - [Umami Documentation](https://umami.is/docs) - Full documentation | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "name": "@umami/tracker", | ||
| "version": "3.0.3", | ||
| "description": "Umami analytics tracker core library", | ||
| "author": "Umami Software, Inc. <hello@umami.is>", | ||
| "license": "MIT", | ||
| "homepage": "https://umami.is", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/umami-software/umami.git", | ||
| "directory": "packages/tracker" | ||
| }, | ||
| "type": "module", | ||
| "main": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.js" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "sideEffects": false | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export { createTracker } from './tracker'; | ||
|
|
||
| export type { | ||
| CustomEventFunction, | ||
| EventData, | ||
| EventProperties, | ||
| PageViewProperties, | ||
| TrackedProperties, | ||
| TrackerConfig, | ||
| UmamiTracker, | ||
| WithRequired, | ||
| } from './types'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.