Skip to content

Conversation

@sid597
Copy link
Collaborator

@sid597 sid597 commented Dec 24, 2024

Using guide https://posthog.com/docs/libraries/next-js

To build locally add .env.local file and add the following keys

NEXT_PUBLIC_POSTHOG_KEY=phc_KllQh2hOMmXJ3YwdiLHswb1CfOaEWzoC30mA0u3ZJgp
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com

Its a write only key, Write-only means it can only create new events. It can't read events or any of your other data stored with PostHog, so it's safe to use in public apps.

For website we can do either Product analytics or web analytics there a few more tools but they not provide any useful data for the website.

In a nutshell:

  • Web analytics is good for tracking and monitoring high-level website metrics like page views, bounce rate, and the top sources of traffic. It's a pre-defined dashboard, so it's easy to quickly dip into and find what you need.
  • Product analytics is good for diving deep into how specific users, or groups of users, behave. There are dashboard templates to help you get started, but you can build custom insights and dashboards to your precise needs, and use notebooks for adhoc analysis.

I think product analytics would also not be much useful for the website, we don't have any events that we want to track.

Web analytics gives us insight into:

  • pageview
  • pageleave
  • VIsitors
  • views
  • sessions
  • session duration
  • bounce rate
  • Referrers: Where users are coming from
  • Conversion: A conversion goal is a key event or action you want users to do. It can be seen as the main focus your website is driving towards e.g Signup, Book a demo, Subscribe, Download, Form submission

We have to set up conversion goal (if any) everything else is setup by default.

Summary by CodeRabbit

  • New Features

    • Introduced PostHogPageView and SuspendedPostHogPageView components for tracking page views.
    • Added PostHogProvider component to integrate PostHog analytics into the application.
  • Bug Fixes

    • Ensured required environment variables are checked before initializing PostHog.
  • Chores

    • Added posthog-js dependency for analytics tracking.

@vercel
Copy link

vercel bot commented Dec 24, 2024

@sid597 is attempting to deploy a commit to the Discourse Graphs Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Dec 24, 2024

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

Name Status Preview Comments Updated (UTC)
discourse-graph ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 25, 2024 5:51am

@sid597 sid597 changed the title Initial setup, page views working Posthog on Website: Initial setup, page views working Dec 24, 2024
@sid597 sid597 marked this pull request as draft December 24, 2024 17:37
@sid597 sid597 linked an issue Dec 24, 2024 that may be closed by this pull request
@sid597 sid597 changed the title Posthog on Website: Initial setup, page views working Posthog web analytics in website Dec 24, 2024
@sid597 sid597 marked this pull request as ready for review December 24, 2024 18:02
@sid597 sid597 requested a review from mdroidian December 24, 2024 18:02
@mdroidian
Copy link
Contributor

mdroidian commented Dec 24, 2024

@sid597 is attempting to deploy a commit to the Discourse Graphs Team on Vercel.

A member of the Team first needs to authorize it.

@sid597 Are you using the same email for Vercel and Github?
Could you read up here to see if your git login connection / account settings / etc are correct.
Ideally, this will deploy a preview on a PR request without authorization from another member.

Copy link
Contributor

@mdroidian mdroidian left a comment

Choose a reason for hiding this comment

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

Nice! Just nix those comments and good to go.

I added the env vars to vercel and rebuild the deployment:
https://discourse-graph-git-fork-sid597-posthog-d018e8-discourse-graphs.vercel.app

I think product analytics would also not be much useful for the website, we don't have any events that we want to track.

I agree, web analytics would be good here.

We might look into Product analytics for the plugins.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 25, 2024

Walkthrough

The pull request introduces PostHog analytics integration into a Next.js website application. A new PostHogProvider component is created to configure and initialize PostHog tracking, with a dedicated PostHogPageView component to capture page view events. The implementation ensures proper environment variable configuration and wraps the application's root layout with the analytics provider, enabling comprehensive page view tracking across the website.

Changes

File Changes
apps/website/app/PostHogPageView.tsx - Added new PostHogPageView component for tracking page views
- Created SuspendedPostHogPageView wrapper with Suspense
apps/website/app/layout.tsx - Imported PostHogProvider
- Wrapped children with PostHogProvider
apps/website/app/providers.tsx - Implemented PostHogProvider component
- Added environment variable validation
- Configured PostHog initialization
package.json - Added posthog-js dependency (v1.203.1)

Sequence Diagram

sequenceDiagram
    participant App as Next.js App
    participant Provider as PostHogProvider
    participant PageView as PostHogPageView
    participant PostHog as PostHog Analytics

    App->>Provider: Wrap application
    Provider->>PostHog: Initialize with config
    App->>PageView: Track page changes
    PageView->>PostHog: Send page view event
Loading

Poem

🐰 Hop, hop, analytics track,
PostHog's magic, no turning back!
Page views dancing, metrics so bright,
Rabbit's code leaps with pure delight 🚀
Tracking journeys, one click at a time!


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
apps/website/app/providers.tsx (1)

8-13: Consider logging a more descriptive error message.
Throwing an error here is acceptable for early failure. However, providing environment-specific instructions or a link to documentation would be more helpful for other developers.

 if (
   !process.env.NEXT_PUBLIC_POSTHOG_KEY ||
   !process.env.NEXT_PUBLIC_POSTHOG_HOST
 ) {
-  throw new Error("PostHog environment variables are not set");
+  throw new Error(
+    "PostHog environment variables are missing. Please set NEXT_PUBLIC_POSTHOG_KEY and NEXT_PUBLIC_POSTHOG_HOST."
+  );
 }
apps/website/app/PostHogPageView.tsx (1)

7-25: Check fallback behavior for window.origin.
Though it generally works in a client-side environment, some frameworks or SSR contexts might not define window.origin. Since you’re using "use client", this is likely safe, but keep it in mind if partial SSR is used in the future.

package.json (1)

32-32: Pin the minor version to maintain reproducible builds.
You might consider locking the dependency version to avoid unexpected breaking changes in patch releases, although semver typically handles patch updates gracefully.

 "dependencies": {
-    "posthog-js": "^1.203.1"
+    "posthog-js": "~1.203.1"
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 502d0f6 and 948a8d3.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (4)
  • apps/website/app/PostHogPageView.tsx (1 hunks)
  • apps/website/app/layout.tsx (2 hunks)
  • apps/website/app/providers.tsx (1 hunks)
  • package.json (1 hunks)
🔇 Additional comments (4)
apps/website/app/providers.tsx (1)

15-21: Verify PostHog initialization logic for SSR usage.
Since this code is in a client component, it should not be executed on the server. However, if server-side usage is introduced in the future, consider a guard to ensure PostHog does not run in SSR mode, to avoid potential reference errors.

apps/website/app/PostHogPageView.tsx (1)

7-25: Confirm that capturing a pageview on search parameters change is desired.
Currently, changing only the search parameters triggers an additional pageview event. If this is intentional, great! Otherwise, consider an additional check to prevent multiple captures for minor URL state changes.

Would you like a snippet to handle only path-based events or maintain a custom approach to differentiate between path and search changes?

apps/website/app/layout.tsx (2)

3-3: Ensure consistent naming and usage of the provider.
The import and usage is straightforward. Just confirm that the exported name PostHogProvider from ./providers aligns with naming in code references and documentation.


28-30: Good practice wrapping children within the provider.
This ensures all descendants can access and dispatch analytics events consistently.

@sid597 sid597 merged commit cd6e1f2 into DiscourseGraphs:main Dec 25, 2024
3 checks passed
@sid597 sid597 deleted the posthog-website branch December 26, 2024 02:06
This was referenced Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

Add Analytics

2 participants