Skip to content

Commit

Permalink
Document new enable/disable tracking functions
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Jan 17, 2022
1 parent 0e2eb1b commit 7f37f73
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@ import { trackGoal } from 'fathom-client';
trackGoal('MY_GOAL_CODE', 100);
```

### `enableTrackingForMe()`

Enables tracking for the current visitor.

See https://usefathom.com/docs/features/exclude.

#### Arguments

None.

#### Example

```js
import { enableTrackingForMe } from 'fathom-client';

enableTrackingForMe();
```

### `disableTrackingForMe()`

Disables tracking for the current visitor.

See https://usefathom.com/docs/features/exclude.

#### Arguments

None.

#### Example

```js
import { disableTrackingForMe } from 'fathom-client';

disableTrackingForMe();
```

## Usage

This library is JavaScript framework-agnostic. Below are some usage examples with popular frameworks.
Expand Down
33 changes: 20 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
interface Fathom {
blockTrackingForMe: typeof blockTrackingForMe;
enableTrackingForMe: typeof enableTrackingForMe;
blockTrackingForMe: () => void;
enableTrackingForMe: () => void;
trackPageview: (opts?: PageViewOptions) => void;
trackGoal: (code: string, cents: number) => void;
}
Expand All @@ -24,7 +24,8 @@ export type LoadOptions = {
type FathomCommand =
| { type: 'trackPageview'; opts: PageViewOptions | undefined }
| { type: 'trackGoal'; code: string; cents: number }
| { type: 'blockTrackingForMe' | 'enableTrackingForMe' };
| { type: 'blockTrackingForMe' }
| { type: 'enableTrackingForMe' };

declare global {
interface Window {
Expand Down Expand Up @@ -62,11 +63,11 @@ const flushQueue = (): void => {
window.fathom.trackGoal(command.code, command.cents);
return;

case 'enableTrackingForMe' :
case 'enableTrackingForMe':
window.fathom.enableTrackingForMe();
return;
case 'blockTrackingForMe' :

case 'blockTrackingForMe':
window.fathom.blockTrackingForMe();
return;
}
Expand Down Expand Up @@ -102,18 +103,25 @@ export const load = (siteId: string, opts?: LoadOptions): void => {
tracker.src =
opts && opts.url ? opts.url : 'https://cdn.usefathom.com/script.js';
if (opts) {
if (opts.auto !== undefined) tracker.setAttribute('data-auto', `${opts.auto}`);
if (opts.auto !== undefined)
tracker.setAttribute('data-auto', `${opts.auto}`);
if (opts.honorDNT !== undefined)
tracker.setAttribute('data-honor-dnt', `${opts.honorDNT}`);
if (opts.canonical !== undefined)
tracker.setAttribute('data-canonical', `${opts.canonical}`);
if (opts.includedDomains) {
checkDomainsAndWarn(opts.includedDomains);
tracker.setAttribute('data-included-domains', opts.includedDomains.join(','));
tracker.setAttribute(
'data-included-domains',
opts.includedDomains.join(',')
);
}
if (opts.excludedDomains) {
checkDomainsAndWarn(opts.excludedDomains);
tracker.setAttribute('data-excluded-domains', opts.excludedDomains.join(','));
tracker.setAttribute(
'data-excluded-domains',
opts.excludedDomains.join(',')
);
}
if (opts.spa) tracker.setAttribute('data-spa', opts.spa);
}
Expand Down Expand Up @@ -159,15 +167,14 @@ export const blockTrackingForMe = (): void => {
if (window.fathom) {
window.fathom.blockTrackingForMe();
} else {
enqueue({ type: 'blockTrackingForMe' })
enqueue({ type: 'blockTrackingForMe' });
}
};

export const enableTrackingForMe = (): void => {
if (window.fathom) {
window.fathom.enableTrackingForMe();
} else {
enqueue({ type: 'enableTrackingForMe' })

enqueue({ type: 'enableTrackingForMe' });
}
};
};

0 comments on commit 7f37f73

Please sign in to comment.