Analytics Overview
The SDK tracks e-commerce events and sends them to analytics providers.
Supported Providers
Section titled “Supported Providers”- Google Tag Manager
- Facebook Pixel
- RudderStack
- Custom webhooks
Events follow GA4 specification and can be tracked automatically or manually using two APIs: next.* methods or window.NextAnalytics.
Quick Start
Section titled “Quick Start”Enable analytics with minimal configuration:
window.nextConfig = { apiKey: 'your-api-key', analytics: { enabled: true, mode: 'auto', providers: { gtm: { enabled: true } } }};That’s it! The SDK now automatically tracks:
- User data on every page load
- Product list views on collection pages
- Add to cart events
- Checkout initiation
- Shipping and payment info
How It Works
Section titled “How It Works”1. Events Are Tracked
Section titled “1. Events Are Tracked”The SDK captures e-commerce events using two APIs:
// Simple tracking methodsnext.trackAddToCart(packageId, quantity);next.trackViewItem(packageId);next.trackBeginCheckout();next.trackPurchase(orderData);Works immediately, handles async loading automatically.
// Advanced tracking with full controlwindow.NextAnalytics.trackAddToCart(item, listId, listName);window.NextAnalytics.trackViewItem(item);window.NextAnalytics.track({ event: 'custom_event', data: '...' });Direct access to analytics engine with advanced features.
2. Events Are Stored
Section titled “2. Events Are Stored”All events are stored in three data layers:
window.NextDataLayer- SDK’s primary analytics storewindow.dataLayer- Standard Google Tag Manager layerwindow.ElevarDataLayer- Elevar-compatible format
3. Events Are Sent
Section titled “3. Events Are Sent”Events are automatically sent to all enabled providers:
providers: { gtm: { enabled: true }, // → Google Tag Manager facebook: { enabled: true, settings: {} }, // → Facebook Pixel rudderstack: { enabled: true }, // → RudderStack custom: { enabled: true, settings: {} } // → Your backend}Each provider operates independently - if one fails, others continue working.
Tracking Modes
Section titled “Tracking Modes”Auto Mode (Recommended)
Section titled “Auto Mode (Recommended)”Automatically tracks common e-commerce events:
analytics: { mode: 'auto'}Automatically tracked:
dl_user_data- Page load, route changesdl_view_item_list- Product list viewsdl_view_item- Single product pages, selected items in selectorsdl_add_to_cart- Cart additionsdl_begin_checkout- Checkout startdl_add_shipping_info- Shipping form submissiondl_add_payment_info- Payment form submissiondl_purchase- Order completion
Requires manual tracking:
dl_login/dl_sign_up- User authentication- Custom business events
Event Queue for Redirects:
Purchase events are queued to sessionStorage before page redirect and automatically fired on the confirmation page. This ensures events aren’t lost during navigation.
Manual Mode
Section titled “Manual Mode”Full control over all event tracking:
analytics: { mode: 'manual'}You must manually track ALL events using API methods.
Common Use Cases
Section titled “Common Use Cases”Standard E-commerce with GTM
Section titled “Standard E-commerce with GTM”window.nextConfig = { apiKey: 'your-api-key', analytics: { enabled: true, mode: 'auto', providers: { gtm: { enabled: true } } }};
// Purchase events tracked automatically in auto mode// No manual tracking requiredE-commerce with Facebook Ads
Section titled “E-commerce with Facebook Ads”window.nextConfig = { apiKey: 'your-api-key', storeName: 'my-store', // Required for Facebook deduplication analytics: { enabled: true, mode: 'auto', providers: { gtm: { enabled: true }, facebook: { enabled: true, settings: { pixelId: 'YOUR_PIXEL_ID' } } } }};Multi-Platform Pipeline
Section titled “Multi-Platform Pipeline”analytics: { enabled: true, mode: 'auto', providers: { gtm: { enabled: true }, facebook: { enabled: true, settings: { pixelId: 'xxx' } }, rudderstack: { enabled: true }, custom: { enabled: true, settings: { endpoint: 'https://api.yourapp.com/events' } } }}Event Data Structure
Section titled “Event Data Structure”All events follow GA4-compliant format:
{ event: 'dl_add_to_cart', event_id: 'sess_123_2_1234567890', event_time: '2025-01-12T10:30:00Z',
user_properties: { visitor_type: 'guest', customer_id: 'user_123' },
ecommerce: { currency: 'USD', value: 99.99, items: [{ item_id: 'SKU-123', item_name: 'Product Name', price: 99.99, quantity: 1 }] },
attribution: { utm_source: 'google', utm_medium: 'cpc', funnel: 'main' },
_metadata: { session_id: 'sess_abc123', sequence_number: 2, source: 'next-campaign-cart', version: '0.2.0' }}Testing & Verification
Section titled “Testing & Verification”Enable Debug Mode
Section titled “Enable Debug Mode”analytics: { debug: true}// Enable at runtimewindow.NextAnalytics.setDebugMode(true);
// Check statusconst status = window.NextAnalytics.getStatus();console.log(status);Disable Tracking Temporarily
Section titled “Disable Tracking Temporarily”https://yoursite.com?ignore=trueThis disables ALL tracking for the entire session.
To clear:
window.NextAnalyticsClearIgnore();Verify Events
Section titled “Verify Events”// Check all data layersconsole.log(window.NextDataLayer);console.log(window.dataLayer); // GTMconsole.log(window.ElevarDataLayer); // Elevar
// Get analytics statusconst status = window.NextAnalytics.getStatus();console.log('Events tracked:', status.eventsTracked);console.log('Providers:', status.providers);Next Steps
Section titled “Next Steps”-
Configure providers - Set up GTM, Facebook Pixel, or other platforms
-
Learn tracking methods - Understand the tracking API
-
Review events - See all standard e-commerce events
See Event Reference
-
Advanced tracking - Create custom events and use transform functions
See Custom Events
Documentation Structure
Section titled “Documentation Structure”- Configuration & Modes - Detailed configuration options
- Tracking API Reference - Complete API documentation
- Providers - Provider-specific setup guides
- Event Reference - Complete event schemas and examples
- Custom Events - Advanced tracking patterns
- Debugging - Troubleshooting and testing
- Best Practices - Implementation patterns and tips
Do I need to call next.init()?
Section titled “Do I need to call next.init()?”No! Analytics initializes automatically when the SDK loads. Just configure window.nextConfig.
What’s the difference between the three data layers?
Section titled “What’s the difference between the three data layers?”window.dataLayer- Standard GTM data layerwindow.NextDataLayer- SDK’s primary analytics store (all events)window.ElevarDataLayer- Elevar-compatible format for GTM integrations
Can I track events before SDK loads?
Section titled “Can I track events before SDK loads?”Yes, use the nextReady queue:
window.nextReady = window.nextReady || [];window.nextReady.push(function() { next.trackAddToCart('123', 1);});What happens if a provider fails?
Section titled “What happens if a provider fails?”The SDK handles failures gracefully:
- Events still track to NextDataLayer
- Other providers continue working
- Warnings logged in debug mode
- SDK functionality unaffected
How do I check if analytics is working?
Section titled “How do I check if analytics is working?”// Check statuswindow.NextAnalytics.getStatus();
// Check eventswindow.NextDataLayer;
// Enable debugwindow.NextAnalytics.setDebugMode(true);