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

(Example) Update with-segment-analytics to use segmentio/analytics-next and app layout #52327

Merged
merged 11 commits into from Aug 30, 2023

Conversation

lukebussey
Copy link
Contributor

The with-segment-analytics example is out of date so this PR updates it to use segmentio/analytics-next with TypeScript and the app layout.

@lukebussey lukebussey requested review from a team as code owners July 6, 2023 14:03
@ijjk ijjk added the examples Issue/PR related to examples label Jul 6, 2023
@lukebussey
Copy link
Contributor Author

@silesky do I need to do anything different here?

@silesky
Copy link

silesky commented Jul 16, 2023

@silesky do I need to do anything different here?

See my comment 😄

@lukebussey
Copy link
Contributor Author

@silesky how's it look now?

@silesky
Copy link

silesky commented Aug 4, 2023

@silesky how's it look now?

Looks good to me! I have been busy and haven't had time to pull it down; Did you run it once locally to see if it works as expected? (just checking). If so, I'm ready to give my ✔️

@lukebussey
Copy link
Contributor Author

@silesky how's it look now?

Looks good to me! I have been busy and haven't had time to pull it down; Did you run it once locally to see if it works as expected? (just checking). If so, I'm ready to give my ✔️

Yep, looks good to me

@@ -1,36 +0,0 @@
import Page from '../components/Page'
Copy link
Contributor

Choose a reason for hiding this comment

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

Next.js applications still exist that use the pages router. It would be a good idea to update the documentation for it rather than delete it outright. At the very least, I'd recommend updating the REAMDE to incuding Segment's own example, but there's looks a bit complicated... For example, it's not strictly necessary to use partytown or a context provider like they do.

Copy link
Contributor Author

@lukebussey lukebussey Aug 9, 2023

Choose a reason for hiding this comment

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

I'd be happy to link to their example, but I do agree it is a bit complicated. Alternatively I would also be happy to add a separate simpler example for using Segment with the pages router also.

Copy link
Contributor

Choose a reason for hiding this comment

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

I only happened upon this as I'm updating stuff for work. I feel lucky to have bumped into it while you're devving 😅

Curious... how different do you think the pages directory setup would be from what you have here? Is Analytics.load safe to call in the clear or is Segment's example usage of ContextProvider a necessity to avoid undefined references of window?

Copy link

@silesky silesky Aug 10, 2023

Choose a reason for hiding this comment

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

Segment's example usage of ContextProvider a necessity to avoid undefined references of window?

Segmenter here. We only use it in our example because it's more of a playground, so you can do stuff you'd never do in a normal application, like dynamically update the writeKey. See the little warning in our README. I would like to rename our Segment example to something like "next-12-playground" to make it clear it's a bit old and not a clean example.

I'm excited about this example and intend to link to it in the Segment docs/analytics readme as the source of truth for a clean Next.js 13 implementation.

PS
Open Q: if it is worth creating a clean standalone page router implementation, and if that should live here or elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's totally fine to have it live here and differentiate the example by name:

with-segment
with-segment-pages-router

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just added a pages router example

Comment on lines 6 to 10
const { asPath } = useRouter()

useEffect(() => {
analytics.page()
}, [asPath])
Copy link
Contributor

@kylemh kylemh Aug 10, 2023

Choose a reason for hiding this comment

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

https://nextjs.org/docs/pages/api-reference/functions/use-router#routerevents

This might be better to do because asPath might represent the actual file path instead of a changed route. If you want to keep using asPath, I'd conditionally call page() only if router.isReady is true.

Suggested change
const { asPath } = useRouter()
useEffect(() => {
analytics.page()
}, [asPath])
const router = useRouter()
useEffect(() => {
analytics.page(); // capture initial route
router.events.on('routeChangeComplete', () => analytics.page());
return () => {
router.events.off('routeChangeComplete', () => analytics.page());
}
}, [])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do you get the initial page view using router events? It only seems to fire after the initial page view.

Copy link
Contributor

Choose a reason for hiding this comment

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

updated example. Alternatively, you can stick with asPath, I'd just do so conditionally after router.isReady is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got it working with routeChangeComplete thanks @kylemh

@kylemh
Copy link
Contributor

kylemh commented Aug 14, 2023

@leerob this is the PR I was telling you about.

leerob
leerob previously approved these changes Aug 14, 2023
@leerob
Copy link
Member

leerob commented Aug 14, 2023

Could you fix the ESLint errors please and we can merge? 🙏

@lukebussey
Copy link
Contributor Author

Fixed by adding router.events to the dependency array

@lukebussey
Copy link
Contributor Author

@leerob @silesky is there anything into else I need to do to get this merged?

@silesky
Copy link

silesky commented Aug 20, 2023

@leerob @silesky is there anything into else I need to do to get this merged?

LGTM, you've got my 👍

@ijjk
Copy link
Member

ijjk commented Aug 30, 2023

Allow CI Workflow Run

  • approve CI run for commit: b46dfd0

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

1 similar comment
@ijjk
Copy link
Member

ijjk commented Aug 30, 2023

Allow CI Workflow Run

  • approve CI run for commit: b46dfd0

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@leerob leerob merged commit 1aab5ee into vercel:canary Aug 30, 2023
47 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples locked
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants