Allows you to send page view and event data from your Discourse site to Segment using device mode tracking for real-time analytics.
See How do I install a Theme or Theme Component?
As of 2025, all JavaScript for this theme component is located in:
javascripts/discourse/api-initializers/init-theme.js
This follows the latest Discourse theme best practices. The old <script type='text/discourse-plugin'>
block is no longer used and has been removed.
- Go to Admin > Customize > Themes > [Your Theme] > Components > discourse-segment-theme-component > Settings.
- Enter your Segment Write Key in the
segment_write_key
field. - Choose your User ID Strategy from the dropdown:
email
- Use user email as UserID (recommended)external_id
- Use SSO external_id (if SSO enabled)anonymous_id
- Use anonymous ID onlydiscourse_id
- Use internal Discourse user ID
- Configure tracking options:
include_user_email
- Include user email in identify payloadtrack_users
- Track user logins, refreshes, and sign-upstrack_page
- Track page visits with descriptive page namestrack_topic_creation
- Track topic creation and tag changestrack_post_creation
- Track post creationtrack_likes
- Track post likestrack_flags
- Track post and topic flagstrack_bookmarks
- Track post and topic bookmarks
Choose how users are identified in Segment:
Option | Description | Use Case |
---|---|---|
email |
User's email address | Works across sources |
external_id |
SSO external ID | When using SSO and want consistent IDs |
anonymous_id |
Use anonymous ID only | Preserves User ID avoiding mismatch |
discourse_id |
Internal Discourse ID | Simple numeric IDs, least privacy-friendly |
Note: The external_id
option requires SSO to be enabled.
Anonymous ID: Uses the anonymous_id only allowing you to merge on email which is now included in context.traits.email.
Tip: To carry your anonymous_id from your site, use the Segment Querystring API.
identify
- User identification with flexible ID strategytrack("Signed Up")
- New user registration
track("Topic Created")
- New topic creation with category and tag datatrack("Post Created")
- New post creation with topic contexttrack("Topic Tag Created")
- When tags are added to topics
track("Post Liked")
- Post likes with full contexttrack("Post Bookmarked")
/track("Topic Bookmarked")
- Bookmark actionstrack("Post Flagged")
/track("Topic Flagged")
- Flag actions with reasons
page("Latest Topics")
- Homepage visitspage("All Categories")
- Category listingpage("Category: [Name]")
- Individual category pagespage("Topic Viewed")
- Topic viewing with title and IDpage("User Profile")
- User profile pagespage("Admin Dashboard")
- Admin interfacepage("Tag: [Name]")
- Tag pages
All events include consistent base properties for device mode tracking:
platform
- Device platform (Web, iOS, Android)timestamp
- ISO timestamp of the eventdiscourse_user_id
- Current user ID if usedlocation
- Current page contextcontext.traits.email
- User email (when available) for better mergingcontext.traits.discourse_username
- Discourse username (when available) for better merging
Additional properties vary by event type and include relevant IDs, names, slugs, and metadata.
The component automatically configures Content Security Policy (CSP) to allow Segment's CDN:
- Domain:
https://cdn.segment.com/analytics.js
- Covers: All Segment write keys and analytics.min.js files
- Security: Maintains Discourse's security while enabling tracking
The Segment analytics.js snippet is dynamically loaded using your configured write key. No API keys are hardcoded in the component.
- Real-time tracking - Events fire immediately without server round-trips
- No server dependencies - Works independently of Discourse backend
- Rich context - Full browser and user context available
- Immediate feedback - See events in Segment debugger instantly
- Better merging - Email in context.traits helps merge user profiles across sessions
- Make changes to your settings as needed.
- The component supports calling
segment.identify
when a user first logs on to the site. - Tracks comprehensive user engagement across all major Discourse activities.
Note: For development, use the Discourse Ember CLI server (http://localhost:4200
) for hot reloading and best results.
Compatibility: This component provides the same comprehensive tracking as the discourse-segment-CDP plugin but uses frontend device mode tracking instead of backend Ruby tracking.