Skip to content

Commit

Permalink
Simplify next example
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer authored Feb 3, 2020
1 parent a397251 commit 5853c02
Showing 1 changed file with 20 additions and 48 deletions.
68 changes: 20 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,57 +47,29 @@ const onSignUp = () => {
Create an `_app.js` file in your `pages` directory, [like this](https://nextjs.org/docs#custom-app):

```jsx
import React from 'react';
import App from 'next/app';

class MyApp extends App {
render() {
const { Component, pageProps } = this.props;
return <Component {...pageProps} />;
}
}

export default MyApp;
```
import React, { useEffect } from 'react';
import Router from 'next/router';
import * as Fathom from 'fathom-client';

Then, add a wrapper component with an effect to load Fathom on page load:

```diff
- import React from 'react'
+ import React, { useEffect } from 'react'
+ import * as Fathom from 'fathom-client'
+ import Router from 'next/router'
import App from 'next/app'

+ Router.events.on('routeChangeComplete', () => {
+ Fathom.trackPageview();
+ });

+ function Layout(props) {
+ useEffect(() => {
+ if (process.env.NODE_ENV === 'production') {
+ Fathom.load();
+ Fathom.setSiteId('<your-site-id>');
+ Fathom.trackPageview();
+ }
+ }, []);
+
+ return <div {...props} />;
+ }

class MyApp extends App {
render() {
const { Component, pageProps } = this.props
- return <Component {...pageProps} />
+ return (
+ <Layout>
+ <Component {...pageProps}></Component>
+ </Layout>
+ )
// Record a pageview when route changes
Router.events.on('routeChangeComplete', () => {
Fathom.trackPageview();
});

function App({ Component, pageProps }) {
// Initialize Fathom when the app loads
useEffect(() => {
if (process.env.NODE_ENV === 'production') {
Fathom.load();
Fathom.setSiteId('ZFEWBXJZ');
Fathom.trackPageview();
}
}
}, []);

return <Component {...pageProps} />;
}

export default MyApp
export default App;
```

## Releasing
Expand Down

0 comments on commit 5853c02

Please sign in to comment.