Analytics Setup — us.buywhere.com
Overview
BuyWhere uses Google Tag Manager (GTM) to manage tracking tags including Google Analytics 4 (GA4). This setup enables comprehensive tracking of user interactions across the site for US launch analytics.
Configuration
Environment Variables
Add the following to your .env.local file in /home/paperclip/buywhere-api/frontend/:
# Google Tag Manager Container ID
NEXT_PUBLIC_GTM_ID=GTM-XXXXXXX
# Google Analytics 4 Measurement ID (configured within GTM)
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXX
GTM Container Setup
- Create a GTM container at Tag Manager
- Set the container ID in
NEXT_PUBLIC_GTM_ID - Within GTM, configure:
- GA4 Configuration tag: Set Measurement ID to your GA4 property
- Enable Enhanced Measurement for automatic pageviews, scroll tracking, outbound clicks, site search, and video engagement
- Set Cross-domain tracking if needed for affiliate redirects
- Create triggers for custom events (see Events section below)
Events Tracked
Automatic (via GTM/GA4 Enhanced Measurement)
| Event | Trigger | GA4 Parameter |
|---|---|---|
| page_view | Page load | page_location, page_title |
| scroll | 25/50/75/90% depth | scrolled_to |
| outbound_click | Link to external domain | link_url |
| site_search | Search query param | search_term |
| video_engagement | Video play/progress | video_percent |
Custom Events
| Event Name | Category | Tracked On | Parameters |
|---|---|---|---|
search | engagement | Search form submission | event_label (query), value (result count) |
select_item | engagement | Compare page retailer click | items[] (product info) |
click_affiliate | conversion | Affiliate link click | event_label (product - retailer) |
sign_up | conversion | US waitlist signup | event_label (method - source) |
Event Details
search
Fires when a user submits a search query.
gtag('event', 'search', {
event_category: 'engagement',
event_label: '<search_term>',
value: <result_count>
});
select_item (Price Comparison Click)
Fires when a user clicks on a retailer from the comparison table.
gtag('event', 'select_item', {
event_category: 'engagement',
event_label: '<retailer_name>',
items: [{
item_id: '<product_name>',
item_name: '<product_name>',
price: <price>
}]
});
click_affiliate
Fires when a user clicks an affiliate link on the product page.
gtag('event', 'click_affiliate', {
event_category: 'conversion',
event_label: '<product_name> - <retailer_name>'
});
sign_up
Fires when a user successfully subscribes to the US waitlist.
gtag('event', 'sign_up', {
event_category: 'conversion',
event_label: 'email - us-waitlist'
});
Verification
GA4 DebugView
- Install GA4 DebugView Chrome Extension
- Enable Debug mode on the site
- Navigate and interact with the site
- View events in real-time at DebugView
GTM Preview Mode
- Open GTM container and click Preview
- Enter your site URL to connect
- Interact with the site to see tags firing
- Check the DataLayer tab to see event payloads
File Structure
frontend/
├── app/
│ ├── layout.tsx # GTM snippet + AnalyticsTracker
│ └── ...
├── components/
│ └── AnalyticsTracker.tsx # Client-side pageview tracking
├── lib/
│ └── ga4.ts # GA4/GTM event tracking functions
├── .env.example # Environment variable template
└── public/
└── sitemap-us.xml # US site sitemap
Server-Side Conversion Tracking (Measurement Protocol)
BuyWhere also uses GA4 Measurement Protocol for server-side conversion tracking, which captures events that client-side tracking cannot (e.g., affiliate redirects via 302, API-based signups).
Environment Variables
Add to your .env file in /home/paperclip/buywhere-api/:
# GA4 Measurement Protocol (server-side conversion tracking)
# Get Measurement ID from GA4 Admin → Data Streams → Web Stream → Measurement ID
GA4_MEASUREMENT_ID=G-XXXXXXXXXX
# Generate API secret from GA4 Admin → Data Streams → Web Stream → Measurement Protocol → Create
GA4_API_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Set to "true" to disable GA4 tracking (e.g., in dev)
GA4_DISABLED=false
Conversion Events Tracked (Server-Side)
| Event Name | Trigger | Platform Filter | Parameters |
|---|---|---|---|
affiliate_click | Product click redirect to US retailer | US platforms only (amazon_us, walmart_us, bestbuy_us, target_us) | tracking_id, product_id, platform, affiliate_url, api_key_id, agent_id |
price_alert_set | Price alert creation | All platforms | product_id, product_name, target_price, currency, api_key_id, agent_id |
signup_complete | Developer/API signup | All platforms | plan, source, utm_source, utm_medium, utm_campaign |
Marking Events as Conversions in GA4 Admin
After server-side events are flowing, mark them as conversions:
- Go to GA4 Admin → Conversions
- Click New conversion event
- Enter the exact event name:
affiliate_clickprice_alert_setsignup_complete
- Click Save
Wait 24-48 hours for events to appear as available for conversion marking (GA4 processing delay).
GTM Trigger Setup (Client-Side)
If using GTM for client-side event capture alongside Measurement Protocol:
- Open your GTM container
- Go to Triggers → New
- Create a Custom Event trigger for each:
- Event name:
affiliate_click(orclick_affiliatefor client-side) - Trigger fires on:
Custom Eventwith regex match.*
- Event name:
- Attach these triggers to your GA4 Event tag
- Mark as conversion: GA4 Admin → Conversions → New conversion event (same as above)
Creating the High-Intent Users Audience
Create a GA4 audience to track users with 1+ affiliate clicks in 7 days:
- Go to GA4 Admin → Audiences
- Click New Audience
- Click Create a custom audience
- Configure:
- Audience name:
high-intent users - Conditions:
- Add condition:
Event→affiliate_click - Count matches:
at least 1 - Time range:
7 days
- Add condition:
- Audience name:
- (Optional) Add additional conditions like
Session→CountryequalsUnited States - Click Save
This audience powers US revenue ROI reporting and can be used for remarketing.
Key Implementation Notes
- GTM loads asynchronously; custom events use
window.dataLayerqueue - The
AnalyticsTrackercomponent (inlayout.tsx) initializes GA and tracks page views - The
gtagfunction is shimmed to push events todataLayerfor GTM processing - GA4 is configured with
send_page_view: false— page views are tracked via theAnalyticsTrackercomponent - All custom events follow GA4's recommended naming and parameter conventions
- Server-side Measurement Protocol tracking (
app/services/analytics/ga4.py) captures events that client-side cannot, particularly affiliate click redirects which use 302 redirects