Skip to main content

What Are Template Events?

Template events are pre-built methods for common tracking scenarios. Instead of remembering property names and structure, use ready-made methods: Without templates:
With templates:
Same result, but template methods guide you with type hints and ensure consistency.

Why Use Templates?

Type Safety: TypeScript knows which properties are available:
Consistency: Everyone on your team tracks events the same way. Documentation: Method names make it clear what you’re tracking. You can still use grain.track() for custom events. Templates are optional helpers for common cases.

Authentication Events

trackLogin()

Track when users log in:
Common properties:
  • method: ‘email’, ‘google’, ‘github’, etc.
  • success: Whether login succeeded
  • errorMessage: If login failed, why?
  • rememberMe: Whether “remember me” was checked
  • twoFactorEnabled: If 2FA was used
Example with error:

trackSignup()

Track new user registrations:
Common properties:
  • method: How they signed up
  • source: Where they came from
  • plan: Which plan they chose
  • success: Whether signup completed
  • referralCode: If they used a referral

E-commerce Events

trackCheckout()

Track when users start or complete checkout:
Common properties:
  • orderId: Unique order identifier
  • total: Total amount
  • currency: Currency code (USD, EUR, etc.)
  • items: Array of products (see below)
  • paymentMethod: How they’re paying
  • couponCode: Any discount codes used
With items:

trackPurchase()

Track completed purchases:
Similar to checkout, but specifically for completed transactions. Track both to measure checkout completion rate.

trackAddToCart()

Track when items are added to cart:

trackRemoveFromCart()

Track when items are removed:

trackPageView()

Track page views:
Common properties:
  • page: Page path
  • title: Page title
  • url: Full URL
  • referrer: Where user came from
SPA (Single Page App) example:

trackSearch()

Track search queries:
Common properties:
  • query: What user searched for
  • results: Number of results found
  • filters: Any filters applied
  • sortBy: Sort order used

Using Template Events

All template methods work like track():

Creating Custom Events

For events not covered by templates, use track():
Templates are just helpers. You’re never limited to them.

Real-World Example

Here’s a complete user flow with template events:
Now you can analyze the complete journey from landing to purchase.

Available Templates

Current template methods:
  • trackLogin() - User login
  • trackSignup() - User registration
  • trackCheckout() - Checkout process
  • trackPurchase() - Completed purchase
  • trackAddToCart() - Add to cart
  • trackRemoveFromCart() - Remove from cart
  • trackPageView() - Page views
  • trackSearch() - Search queries
More templates may be added based on common use cases.

Next Steps

E-commerce Guide

Complete e-commerce tracking guide

Template Methods API

Full API reference for templates

Event Tracking

Learn more about custom events