fathom-client
Advanced tools
Comparing version 2.0.3 to 3.0.0
@@ -5,38 +5,103 @@ 'use strict'; | ||
var getFathom = function getFathom() { | ||
return window.fathom = window.fathom || function () { | ||
(window.fathom.q = window.fathom.q || []).push(arguments); | ||
}; | ||
// refer to https://usefathom.com/support/tracking-advanced | ||
/** | ||
* Enqueues a command to dispatch to fathom when the library is loaded. | ||
* | ||
* @param command - A set of arguments to dispatch to fathom later. | ||
*/ | ||
var enqueue = function enqueue(command) { | ||
window.__fathomClientQueue = window.__fathomClientQueue || []; | ||
window.__fathomClientQueue.push(command); | ||
}; | ||
/** | ||
* Flushes the command queue. | ||
*/ | ||
var load = function load() { | ||
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '//cdn.usefathom.com/tracker.js'; | ||
window.fathom = window.fathom || function () { | ||
(window.fathom.q = window.fathom.q || []).push(arguments); | ||
}; | ||
var flushQueue = function flushQueue() { | ||
window.__fathomClientQueue = window.__fathomClientQueue || []; | ||
window.__fathomClientQueue.forEach(function (command) { | ||
switch (command.type) { | ||
case 'trackPageview': | ||
if (command.opts) { | ||
window.fathom.trackPageview(command.opts); | ||
} else { | ||
window.fathom.trackPageview(); | ||
} | ||
return; | ||
case 'trackGoal': | ||
window.fathom.trackGoal(command.code, command.cents); | ||
return; | ||
} | ||
}); | ||
window.__fathomClientQueue = []; | ||
}; | ||
var load = function load(siteId, opts) { | ||
var tracker = document.createElement('script'); | ||
var firstScript = document.getElementsByTagName('script')[0]; | ||
tracker.id = 'fathom-script'; | ||
tracker.async = true; | ||
tracker.src = url; | ||
tracker.id = 'fathom-script'; | ||
tracker.setAttribute('site', siteId); | ||
tracker.src = opts && opts.url ? opts.url : 'https://cdn.usefathom.com/script.js'; | ||
if (opts) { | ||
if (opts.auto !== undefined) tracker.setAttribute('auto', "".concat(opts.auto)); | ||
if (opts.honorDNT !== undefined) tracker.setAttribute('honor-dnt', "".concat(opts.honorDNT)); | ||
if (opts.canonical !== undefined) tracker.setAttribute('canonical', "".concat(opts.canonical)); | ||
if (opts.includedDomains) tracker.setAttribute('included-domains', opts.includedDomains.join(',')); | ||
if (opts.excludedDomains) tracker.setAttribute('excluded-domains', opts.excludedDomains.join(',')); | ||
if (opts.spa) tracker.setAttribute('spa', opts.spa); | ||
} | ||
tracker.onload = flushQueue; | ||
firstScript.parentNode.insertBefore(tracker, firstScript); | ||
}; | ||
var setSiteId = function setSiteId(siteId) { | ||
var fathom = getFathom(); | ||
fathom('set', 'siteId', siteId); | ||
/** | ||
* Tracks a pageview. | ||
* | ||
* @param opts - An optional `url` or `referrer` to override auto-detected values. | ||
*/ | ||
var trackPageview = function trackPageview(opts) { | ||
if (window.fathom) { | ||
if (opts) { | ||
window.fathom.trackPageview(opts); | ||
} else { | ||
window.fathom.trackPageview(); | ||
} | ||
} else { | ||
enqueue({ | ||
type: 'trackPageview', | ||
opts: opts | ||
}); | ||
} | ||
}; | ||
var trackPageview = function trackPageview() { | ||
var fathom = getFathom(); | ||
fathom('trackPageview'); | ||
/** | ||
* Tracks a goal. | ||
* | ||
* @param code - The goal ID. | ||
* @param cents - The value in cents. | ||
*/ | ||
var trackGoal = function trackGoal(code, cents) { | ||
if (window.fathom) { | ||
window.fathom.trackGoal(code, cents); | ||
} else { | ||
enqueue({ | ||
type: 'trackGoal', | ||
code: code, | ||
cents: cents | ||
}); | ||
} | ||
}; | ||
var trackGoal = function trackGoal(id, cents) { | ||
var fathom = getFathom(); | ||
fathom('trackGoal', id, cents); | ||
}; | ||
exports.load = load; | ||
exports.setSiteId = setSiteId; | ||
exports.trackGoal = trackGoal; | ||
exports.trackPageview = trackPageview; |
@@ -1,34 +0,100 @@ | ||
var getFathom = function getFathom() { | ||
return window.fathom = window.fathom || function () { | ||
(window.fathom.q = window.fathom.q || []).push(arguments); | ||
}; | ||
// refer to https://usefathom.com/support/tracking-advanced | ||
/** | ||
* Enqueues a command to dispatch to fathom when the library is loaded. | ||
* | ||
* @param command - A set of arguments to dispatch to fathom later. | ||
*/ | ||
var enqueue = function enqueue(command) { | ||
window.__fathomClientQueue = window.__fathomClientQueue || []; | ||
window.__fathomClientQueue.push(command); | ||
}; | ||
/** | ||
* Flushes the command queue. | ||
*/ | ||
var load = function load() { | ||
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '//cdn.usefathom.com/tracker.js'; | ||
window.fathom = window.fathom || function () { | ||
(window.fathom.q = window.fathom.q || []).push(arguments); | ||
}; | ||
var flushQueue = function flushQueue() { | ||
window.__fathomClientQueue = window.__fathomClientQueue || []; | ||
window.__fathomClientQueue.forEach(function (command) { | ||
switch (command.type) { | ||
case 'trackPageview': | ||
if (command.opts) { | ||
window.fathom.trackPageview(command.opts); | ||
} else { | ||
window.fathom.trackPageview(); | ||
} | ||
return; | ||
case 'trackGoal': | ||
window.fathom.trackGoal(command.code, command.cents); | ||
return; | ||
} | ||
}); | ||
window.__fathomClientQueue = []; | ||
}; | ||
var load = function load(siteId, opts) { | ||
var tracker = document.createElement('script'); | ||
var firstScript = document.getElementsByTagName('script')[0]; | ||
tracker.id = 'fathom-script'; | ||
tracker.async = true; | ||
tracker.src = url; | ||
tracker.id = 'fathom-script'; | ||
tracker.setAttribute('site', siteId); | ||
tracker.src = opts && opts.url ? opts.url : 'https://cdn.usefathom.com/script.js'; | ||
if (opts) { | ||
if (opts.auto !== undefined) tracker.setAttribute('auto', "".concat(opts.auto)); | ||
if (opts.honorDNT !== undefined) tracker.setAttribute('honor-dnt', "".concat(opts.honorDNT)); | ||
if (opts.canonical !== undefined) tracker.setAttribute('canonical', "".concat(opts.canonical)); | ||
if (opts.includedDomains) tracker.setAttribute('included-domains', opts.includedDomains.join(',')); | ||
if (opts.excludedDomains) tracker.setAttribute('excluded-domains', opts.excludedDomains.join(',')); | ||
if (opts.spa) tracker.setAttribute('spa', opts.spa); | ||
} | ||
tracker.onload = flushQueue; | ||
firstScript.parentNode.insertBefore(tracker, firstScript); | ||
}; | ||
var setSiteId = function setSiteId(siteId) { | ||
var fathom = getFathom(); | ||
fathom('set', 'siteId', siteId); | ||
/** | ||
* Tracks a pageview. | ||
* | ||
* @param opts - An optional `url` or `referrer` to override auto-detected values. | ||
*/ | ||
var trackPageview = function trackPageview(opts) { | ||
if (window.fathom) { | ||
if (opts) { | ||
window.fathom.trackPageview(opts); | ||
} else { | ||
window.fathom.trackPageview(); | ||
} | ||
} else { | ||
enqueue({ | ||
type: 'trackPageview', | ||
opts: opts | ||
}); | ||
} | ||
}; | ||
var trackPageview = function trackPageview() { | ||
var fathom = getFathom(); | ||
fathom('trackPageview'); | ||
/** | ||
* Tracks a goal. | ||
* | ||
* @param code - The goal ID. | ||
* @param cents - The value in cents. | ||
*/ | ||
var trackGoal = function trackGoal(code, cents) { | ||
if (window.fathom) { | ||
window.fathom.trackGoal(code, cents); | ||
} else { | ||
enqueue({ | ||
type: 'trackGoal', | ||
code: code, | ||
cents: cents | ||
}); | ||
} | ||
}; | ||
var trackGoal = function trackGoal(id, cents) { | ||
var fathom = getFathom(); | ||
fathom('trackGoal', id, cents); | ||
}; | ||
export { load, setSiteId, trackGoal, trackPageview }; | ||
export { load, trackGoal, trackPageview }; |
interface Fathom { | ||
(...args: any[]): void; | ||
q?: Array<IArguments>; | ||
trackPageview: (opts?: PageViewOptions) => void; | ||
trackGoal: (code: string, cents: number) => void; | ||
} | ||
export declare type PageViewOptions = { | ||
url?: string; | ||
referrer?: string; | ||
}; | ||
export declare type LoadOptions = { | ||
url?: string; | ||
auto?: boolean; | ||
honorDNT?: boolean; | ||
canonical?: boolean; | ||
includedDomains?: string[]; | ||
excludedDomains?: string[]; | ||
spa?: 'auto' | 'history' | 'hash'; | ||
}; | ||
declare type FathomCommand = { | ||
type: 'trackPageview'; | ||
opts: PageViewOptions | undefined; | ||
} | { | ||
type: 'trackGoal'; | ||
code: string; | ||
cents: number; | ||
}; | ||
declare global { | ||
interface Window { | ||
fathom: Fathom; | ||
fathom?: Fathom; | ||
__fathomClientQueue: FathomCommand[]; | ||
} | ||
} | ||
export declare const load: (url?: string) => void; | ||
export declare const setSiteId: (siteId: string) => void; | ||
export declare const trackPageview: () => void; | ||
export declare const trackGoal: (id: string, cents: number) => void; | ||
export declare const load: (siteId: string, opts?: LoadOptions) => void; | ||
/** | ||
* Tracks a pageview. | ||
* | ||
* @param opts - An optional `url` or `referrer` to override auto-detected values. | ||
*/ | ||
export declare const trackPageview: (opts?: PageViewOptions) => void; | ||
/** | ||
* Tracks a goal. | ||
* | ||
* @param code - The goal ID. | ||
* @param cents - The value in cents. | ||
*/ | ||
export declare const trackGoal: (code: string, cents: number) => void; | ||
export {}; |
{ | ||
"name": "fathom-client", | ||
"version": "2.0.3", | ||
"version": "3.0.0", | ||
"description": "A simple wrapper around the Fathom Analytics library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# Fathom Client [![CircleCI](https://circleci.com/gh/unstacked/fathom-client.svg?style=svg)](https://circleci.com/gh/unstacked/fathom-client) | ||
A [Fathom Analytics](https://usefathom.com/) library for environments with client-side routing. | ||
This library is a JavaScript client for [Fathom Analytics](https://usefathom.com/). It provides a `load` function that will asynchronously inject the Fathom `<script>` tag and tracking functions (`trackPageview` and `trackGoal`) that you can safely call anytime (even if the Fathom script has not yet finished loading). | ||
Extracted from the [StaticKit](https://statickit.com) website. | ||
## Upgrading to 3.x | ||
The 3.0 release comes with a new way to load Fathom: | ||
```diff | ||
- Fathom.load(); | ||
- Fathom.setSiteId('MY_FATHOM_ID'); | ||
+ Fathom.load('MY_FATHOM_ID'); | ||
``` | ||
The `load` function also accepts an object of options: | ||
```js | ||
Fathom.load('MY_FATHOM_ID', { | ||
excludedDomains: ['localhost'] | ||
}); | ||
``` | ||
See [advanced options for tracking](https://usefathom.com/support/tracking-advanced). | ||
## Installation | ||
@@ -28,5 +48,3 @@ | ||
// Upon initial page load... | ||
Fathom.load(); | ||
Fathom.setSiteId('XXXXXXXX'); | ||
Fathom.trackPageview(); | ||
Fathom.load('YOUR_FATHOM_ID'); | ||
@@ -63,7 +81,5 @@ // In the route changed event handler... | ||
useEffect(() => { | ||
if (process.env.NODE_ENV === 'production') { | ||
Fathom.load(); | ||
Fathom.setSiteId('ZFEWBXJZ'); | ||
Fathom.trackPageview(); | ||
} | ||
Fathom.load('ZREWXXJZ', { | ||
excludedDomains: ['localhost'] | ||
}); | ||
}, []); | ||
@@ -70,0 +86,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12090
222
98
1