Analytics & Event Tracking

LeadMachine uses Google Analytics 4 (GA4) to track key user actions and business metrics. This helps you understand user behavior, measure feature adoption, and track business growth.

Technical Documentation - SuperAdmin Access Only


Overview

GA4 tracking is implemented across the application to capture:

  • Revenue events - Subscriptions, payments, cancellations
  • Feature usage - AI enrichment, lead creation, imports
  • Growth metrics - Team invites, company signups, Shopify connections

GA4 Configuration

Setting Up the API Secret

  • Go to GA4 Admin > Data Streams > Web Stream
  • Click Measurement Protocol API secrets
  • Create a new secret
  • Add to your .env file: GA4_API_SECRET=your_secret_here

  • Tracked Events

    Critical (Revenue/Growth)

    High Priority (Feature Usage)

    Medium Priority (Engagement)


    Implementation Details

    Server-Side Tracking (PHP)

    Server-side events use the GA4 Measurement Protocol. The helper is at includes/ga4-tracking.php.

    Example usage:

    php
    require_once __DIR__ . '/includes/ga4-tracking.php';

    // Track a custom event track_ga4_event('custom_event', [ 'company_id' => $company_id, 'custom_param' => 'value' ], null, $user_id);

    // Use helper functions for common events track_ga4_lead_created($company_id, 'manual', $user_id); track_ga4_ai_enrichment('lead', $company_id, $lead_id, $fields_found, $user_id); track_ga4_purchase($company_id, $transaction_id, $value, $quantity, $user_id);

    Client-Side Tracking (JavaScript)

    Client-side events use gtag() which is loaded via header.php. The helper is at assets/js/ga4-tracking.js.

    Example usage:

    javascript
    // Track a custom event
    LM_Analytics.track('custom_event', {
        company_id: 123,
        custom_param: 'value'
    });

    // Use helper methods LM_Analytics.leadCreated(companyId, 'manual'); LM_Analytics.aiEnrichComplete('lead', entityId, companyId, fieldsFound); LM_Analytics.checkoutStart(companyId, userCount);


    GA4 Reports

    Recommended Conversions

    Mark these events as Conversions in GA4 Admin:

  • purchase
  • checkout_start
  • company_created
  • team_member_invited
  • Useful Custom Reports

  • Feature Adoption Funnel
  • - company_createdlead_createdai_enrich_leadpurchase

  • AI Usage by Company
  • - Group by company_id, count ai_enrich_lead + ai_enrich_company

  • Revenue Tracking
  • - Filter by purchase, sum value parameter

  • Team Growth
  • - Count team_member_invited events over time


    Debugging

    Events without an API secret configured will log to PHP error log:

    
    GA4: Skipping event 'event_name' - no API secret configured
    

    To test events:

  • Go to GA4 > Configure > DebugView
  • Enable debug mode in your browser (GA4 browser extension)
  • Events should appear within seconds

  • Files Reference

    SettingValue
    ----------------
    Measurement IDG-6B3D6J11MZ
    API SecretSet via GA4_API_SECRET environment variable
    EventDescriptionLocationKey Parameters
    ----------------------------------------------
    purchaseSubscription completedbilling-success.phptransaction_id, value, currency, items
    checkout_startStarted checkout flowcreate-checkout-session.phpcompany_id, user_count
    payment_successfulInvoice paid (webhook)stripe-webhook.phpcompany_id, amount, subscription_id
    subscription_canceledSubscription canceledstripe-webhook.phpcompany_id, subscription_id
    company_createdNew company signupcreate-company.phpcompany_id
    EventDescriptionLocationKey Parameters
    ----------------------------------------------
    ai_enrich_leadLead AI enrichment completedenrich-lead.phpcompany_id, entity_id, fields_found
    ai_enrich_companyCompany AI enrichment completedenrich-company.phpcompany_id, entity_id, fields_found
    team_member_invitedTeam member addedinvite-user.phpcompany_id, role, is_new_user
    lead_createdLead manually createdcreate-lead.phpcompany_id, source
    leads_imported_csvCSV import completedimport-csv.phpcompany_id, imported_count, skipped_count
    shopify_connectedShopify store connectedshopify-oauth-callback.phpcompany_id, shop_domain
    EventDescriptionLocationKey Parameters
    ----------------------------------------------
    task_createdTask createdtasks/create.phpcompany_id, related_entity_type
    coupon_appliedCoupon code appliedapply-coupon.phpcompany_id, coupon_code
    FilePurpose
    ---------------
    includes/ga4-tracking.phpPHP helper functions for server-side tracking
    assets/js/ga4-tracking.jsJavaScript helper for client-side tracking
    includes/header.phpLoads gtag.js and ga4-tracking.js

    Last Updated: December 2025