Skip to content

feat(helper): Add Fluent helper methods for common PostHog properties#165

Open
salignatmoandal wants to merge 1 commit intoPostHog:masterfrom
salignatmoandal:feat/property-helpers
Open

feat(helper): Add Fluent helper methods for common PostHog properties#165
salignatmoandal wants to merge 1 commit intoPostHog:masterfrom
salignatmoandal:feat/property-helpers

Conversation

@salignatmoandal
Copy link

Summary

This PR adds fluent helper methods to the Properties type for commonly used PostHog property keys. These helpers improve developer experience by providing type-safe, IDE-friendly methods for setting standard properties like $current_url, $referrer, $title, etc.

Changes

Added Helper Methods (properties.go)

  • WithCurrentURL(url string) - Sets $current_url property
  • WithReferrer(referrer string) - Sets $referrer property
  • WithTitle(title string) - Sets $title property
  • WithPath(path string) - Sets $pathname property
  • WithScreen(width, height int) - Sets $screen_width and $screen_height properties

Updated Examples (examples/capture.go)

  • Updated pageview event example to demonstrate the new helper methods
  • Shows improved readability and consistency with existing fluent API patterns

Why this change?

These helpers remove the need to remember exact property keys
and reduce the risk of typos.

They also make pageview-style events much more readable and
consistent with the existing fluent API.

Benefits

  1. Type Safety: Reduces typos in property keys (e.g., "$current_ur" vs WithCurrentURL())
  2. Better DX: IDE autocomplete suggests available helper methods
  3. Consistency: Aligns with existing fluent API pattern (Set(), Merge())
  4. Readability: More expressive code that clearly indicates intent
  5. Documentation: Helper methods serve as documentation for standard PostHog properties

Example Usage

Before:

Properties: map[string]interface{}{
    "$current_url": "https://example.com/dashboard",
    "$title":       "Dashboard - PostHog",
    "$referrer":    "https://google.com",
}

After

Properties: posthog.NewProperties().
    WithCurrentURL("https://example.com/dashboard").
    WithTitle("Dashboard - PostHog").
    WithReferrer("https://google.com"),
    ```

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.

1 participant