Salesforce and Google Analytics integration

Track your Salesforce Leads in Google Analytics is easy that you can imagine.

Steps:

  • setup Google Analytics

  • create a Trigger

Following code snippet you can use for Trigger.

GaClientId__c is custom field on Lead object that represent Client Id in Google Analytics.

trigger GoogleAnalyticsLeadTrigger on Lead (after insert, after update) {
   for (Lead lead : Trigger.new) {
      Map<String,String> fieldUpdateMap = new Map<String,String>();
      fieldUpdateMap.put('ec', 'Salesforce');
      fieldUpdateMap.put('el', 'Lead');
      String cid = (String)lead.get('GaClientId__c');
      fieldUpdateMap.put('cid', cid);
      
      String industry = (String)lead.get('Industry');
      String country = (String)lead.get('Country');
      if (Trigger.IsInsert) {
            fieldUpdateMap.put('ea', 'Create');
            if (industry != null && industry != '') {
               fieldUpdateMap.put('cd5', industry);
            }
            if (country != null && country != '') {
               fieldUpdateMap.put('cd9', country);
            }
      } else if (Trigger.IsUpdate) {
            fieldUpdateMap.put('ea', 'Update');
            Lead oldlead = Trigger.oldMap.get(lead.Id);
            String oldleadIndustry = (String)oldlead.get('Industry');
            String oldleadCountry = (String)oldlead.get('Country');

            if (oldleadIndustry != industry && industry != null && industry != '') {
               fieldUpdateMap.put('cd5', industry);
            }
            if (oldleadCountry != country && country != null && country != '') {
               fieldUpdateMap.put('cd9', (String)country);
            }
      }

      if (fieldUpdateMap.containsKey('cd5') || fieldUpdateMap.containsKey('cd9')) {
         GoogleAnalyticsService.push(fieldUpdateMap);
      }
   }
}

GoogleAnalyticsService is a Service class that contains commonly used code. You can use it in every trigger.

public class GoogleAnalyticsService {

   private static final String trackingId = 'AA-00000000-0';

   private static String createGaRequestUrl(Map<String,String> fieldUpdateMap) {
      fieldUpdateMap.put('v', '1');
      fieldUpdateMap.put('ni', '1');
      String tid = trackingId;
      fieldUpdateMap.put('tid', tid);
      fieldUpdateMap.put('t', 'event');

      System.PageReference pageReference = new System.PageReference('');
      pageReference.getParameters().putAll(fieldUpdateMap);
      return 'https://www.google-analytics.com/collect' + pageReference.getUrl();
   }

   @future (callout=true)
   Public static void push(Map<String, String> fieldUpdateMap) {
      String url = createGaRequestUrl(fieldUpdateMap);

      GaConnectorLogger.getInstance().add(url, fieldUpdateMap.get('el'), fieldUpdateMap.get('ea'));

      Http h = new Http();
      HttpRequest req = new HttpRequest();
      req.setEndpoint(url);
      req.setMethod('GET');
      h.send(req);

      GaConnectorLogger.getInstance().insertLogs();
   }
}

If you need a help with track your Salesforce data in Google Analytics please contact us.