diff --git a/README.md b/README.md index 0d9d95a..4e73b48 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,36 @@ module.exports = { } ``` +## Client side tracking + +```jsx +import React from "react"; +import { useFathom } from "@raae/gatsby-plugin-fathom"; + +const Example = () => { + const { trackGoal, trackPageview } = useFathom(); + return ( + + trackGoal("YKZ06A57", 100)}>Track goal + + + + trackPageview({ + url: "https://yoursite.com/about", + referrer: "https://referrer.com/yoursite-link", + }) + } + > + Track pageview + + + ); +}; + +export default Example; +``` + ### Available options Check the Fathom [advanced tracking options](https://usefathom.com/support/tracking-advanced) docs for more information on these. diff --git a/demo/src/pages/example.js b/demo/src/pages/example.js new file mode 100644 index 0000000..2c9c1c1 --- /dev/null +++ b/demo/src/pages/example.js @@ -0,0 +1,25 @@ +import React from "react" +import { useFathom } from "@raae/gatsby-plugin-fathom" + +const Example = () => { + const { trackGoal, trackPageview } = useFathom() + return ( + + trackGoal("YKZ06A57", 100)}>Track goal + + + + trackPageview({ + url: "https://yoursite.com/about", + referrer: "https://referrer.com/yoursite-link", + }) + } + > + Track pageview + + + ) +} + +export default Example diff --git a/demo/src/pages/index.js b/demo/src/pages/index.js index fdf8b93..f6e846b 100644 --- a/demo/src/pages/index.js +++ b/demo/src/pages/index.js @@ -1,16 +1,30 @@ import React from "react" import { Link } from "gatsby" +import { useFathom } from "@raae/gatsby-plugin-fathom" import Layout from "../components/layout" -const IndexPage = () => ( - - Hi people - Welcome to your new Gatsby site. - Now go build something great. - Go to page 2 - Go to "Using TypeScript" - -) +const IndexPage = () => { + const { trackGoal, trackPageview } = useFathom() + return ( + + Hi people + Welcome to your new Gatsby site. + Now go build something great. + trackGoal("YKZ06A57", 100)}>Track goal + + + trackPageview({ referrer: "https://example.com" })} + > + Track pageview + + + + Go to page 2 + Go to "Using TypeScript" + + ) +} export default IndexPage diff --git a/gatsby-plugin-fathom/index.js b/gatsby-plugin-fathom/index.js index dc241d5..c77fa50 100644 --- a/gatsby-plugin-fathom/index.js +++ b/gatsby-plugin-fathom/index.js @@ -1 +1,2 @@ //no-op +export * from "./src/useFathom"; diff --git a/gatsby-plugin-fathom/src/useFathom.js b/gatsby-plugin-fathom/src/useFathom.js new file mode 100644 index 0000000..dd95f40 --- /dev/null +++ b/gatsby-plugin-fathom/src/useFathom.js @@ -0,0 +1,48 @@ +/*global fathom */ + +const {GATSBY_FATHOM_DEBUG} = process.env; + +const PREFIX = "[@raae/gatsby-plugin-fathom]"; + +const defaults = { + debug: GATSBY_FATHOM_DEBUG, +}; + +export const useFathom = (props) => { + props = { + ...defaults, + ...props, + }; + + const {debug} = props; + + const log = (message, ...rest) => { + if (debug) { + console.log(`${PREFIX} ${message}`, ...rest); + } + }; + + const logFathomUndefined = () => { + log(`${PREFIX} 'fathom' is undefined`); + }; + + const trackPageview = (...args) => { + if (fathom) { + fathom.trackPageview(...args); + log("Track Pageview", ...args); + } else { + logFathomUndefined(); + } + }; + + const trackGoal = (...args) => { + if (fathom) { + fathom.trackGoal(...args); + log("Track Goal", ...args); + } else { + logFathomUndefined(); + } + }; + + return {trackPageview, trackGoal}; +};
Welcome to your new Gatsby site.
Now go build something great.