Skip to content
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

docs: Create google analytics doc #2137

Closed

Conversation

oluwatobiss
Copy link

@oluwatobiss oluwatobiss commented Aug 8, 2023

Motivation

This PR creates a Google Analytics doc showing how to add Google's Global Site Tag (gtag.js) to a Nextra website.

Issue

Closes #2128

@changeset-bot
Copy link

changeset-bot bot commented Aug 8, 2023

⚠️ No Changeset found

Latest commit: 75532d9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Aug 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nextra ❌ Failed (Inspect) Sep 25, 2023 10:18pm
nextra-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2023 10:18pm

@vercel
Copy link

vercel bot commented Aug 8, 2023

@oluwatobiss is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

@@ -0,0 +1,65 @@
import { Steps } from 'nextra/components'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just example package, you can completely remove this page

@@ -0,0 +1,65 @@
import { Steps } from 'nextra/components'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc related not only to docs theme, but to any theme

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, would moving it under "Guide" be better?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under guide -> advanced


Use [`next/script`](https://nextjs.org/docs/pages/building-your-application/optimizing/scripts) to add your Google Analytics tracking code to the custom App (`pages/_app.mdx`) as follows:

```jsx filename="pages/_app.mdx"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems your example will not fire when the page is changed with client-side rendering, here is official example https://github.com/vercel/next.js/blob/canary/examples/with-google-analytics/pages/_app.js

Copy link
Author

@oluwatobiss oluwatobiss Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the doc with examples that now work with client-side rendering.

@oluwatobiss
Copy link
Author

All done.

Copy link
Contributor

@C-EO C-EO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@B2o5T Looks fine for merging. I see no other problem

inline analytics code to website's head

This update resolves the "Do not add <script> tags
using next/head" error browsers throw.

Closes shuding#2128
Copy link

@basedMcghee basedMcghee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for writing this. Works great.

Here's some unsolicited comments changing everything to typescript since the rest of the project is ts.

Comment on lines +17 to +33
```js filename="lib/gtag.js"
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID

export const pageview = url => {
window.gtag('config', GA_TRACKING_ID, {
page_path: url
})
}

export const event = ({ action, category, label, value }) => {
window.gtag('event', action, {
event_category: category,
event_label: label,
value: value
})
}
```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally would recommend this being ts since the rest of the project is

Suggested change
```js filename="lib/gtag.js"
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID
export const pageview = url => {
window.gtag('config', GA_TRACKING_ID, {
page_path: url
})
}
export const event = ({ action, category, label, value }) => {
window.gtag('event', action, {
event_category: category,
event_label: label,
value: value
})
}
```
```js filename="lib/gtag.ts"
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID
export const pageview = (url: string) => {
window.gtag('config', GA_TRACKING_ID, {
page_path: url
})
}
type EventParams = {
event_category: string;
event_label?: string;
value?: number;
}
export const event = ({ action, params }: { action: string; params: EventParams }) => {
window.gtag("event", action, params);
};


Import the `lib/gtag.js` file. And use the `pageview` function to send pageviews to Google Analytics whenever users navigate to a new page.

```jsx filename="pages/_app.mdx"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be _app.tx

Suggested change
```jsx filename="pages/_app.mdx"
```jsx filename="pages/_app.tsx"


Use [`next/script`](https://nextjs.org/docs/pages/building-your-application/optimizing/scripts) and the [`afterInteractive` script loading strategy](https://nextjs.org/docs/pages/api-reference/components/script#afterinteractive) to add the external Google tag code to your website:

```jsx filename="pages/_app.mdx" {2,30-33}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```jsx filename="pages/_app.mdx" {2,30-33}
```jsx filename="pages/_app.tsx" {2,30-33}


Use [`next/script`](https://nextjs.org/docs/pages/building-your-application/optimizing/scripts) and the [`beforeInteractive` script loading strategy](https://nextjs.org/docs/pages/api-reference/components/script#beforeinteractive) to add the inline Google Tag code to your website's `<head>`:

```jsx filename="pages/_app.mdx" {2,20-29}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```jsx filename="pages/_app.mdx" {2,20-29}
```jsx filename="pages/_app.tsx" {2,20-29}


### Create Logic for Sending Pageview and Events to Google Analytics

Create a `lib/gtag.js` file that exports the following:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Create a `lib/gtag.js` file that exports the following:
Add [@types/gtag.js](https://www.npmjs.com/package/@types/gtag.js)
Create a `lib/gtag.ts` file that exports the following:

@oluwatobiss
Copy link
Author

Thank you for writing this. Works great.

Here's some unsolicited comments changing everything to typescript since the rest of the project is ts.

Thank you, @basedMcghee, for your helpful suggestions. Having TypeScript examples makes sense. However, we don't need to replace the JavaScript. Instead, we can document the two options so that users who do not wish to use TypeScript can still follow along with the JavaScript examples.

@ziegenbalg
Copy link

I've tried these changes and get a syntax error:

 1 │ export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID
 2 │ export const pageview = (url: string) => {
   ·                             ─
 3 │   window.gtag('config', GA_TRACKING_ID, {
 4 │     page_path: url
 5 │   })
   ╰────

Caused by:
    Syntax Error

Import trace for requested module:
./lib/gtag.js
   automatically enabled Fast Refresh for 3 custom loaders
 ⨯ ./lib/gtag.js
Error: 

@cenobitedk
Copy link

This guide is specifically designed to work with Vercel, but it doesn't work under different conditions.
lib/gtag.js is inlined during build-time, not at runtime, so this would only work if the image is build on the server. In many cases this is not true. Image is often build on CI (for multiple environments) where the env var is not available, which it won't be because you don't want to build different images for different environments.

@dimaMachina
Copy link
Collaborator

closing since what works with Next.js - works with Nextra, so just follow Next.js Google Analytics examples for Pages router

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add doc for adding google analytics to doc theme
6 participants