I am trying to get data from my Salesforce Communities (napili) into Google Analytics. This Google Analytics guide says this can be done using custom dimensions.

Custom dimensions and metrics allow you to combine Analytics data with non-Analytics data, e.g. CRM data

I tried to put the code that google analytics provides into a lightning component and put that component into the community.

ga('set', 'dimension3', 'custom data');
Error: ga is not defined

I set up google analytics in the community builder but I believe that locker service is blocking the component from accessing that ga object.

I also tried to manually load the google-analytics setup script into my component with ltng:require but even after uploading the js file as a static resource and setting up google-analytics as a CSP trusted site , the script is still throwing CSP errors.

If it is not possible to set custom dimensions, are there any an alternative strategy to get CRM data from salesforce into google-analytics

1 Answer 1


While I have not used my setup in a Community, I have created an Analytics Component that should work for you.

I load analytics as a static resource, and the most important part of the code (initializing and sending some tracking data) is here:

// store the name of the Analytics object
window.GoogleAnalyticsObject = 'ga';
// check whether the Analytics object is defined
if (!('ga' in window)) {
    // define the Analytics object
    window.ga = function() {
        // add the tasks to the queue

    // create the queue
    window.ga.q = [];
// store the current timestamp
window.ga.l = (new Date()).getTime();
window.ga_debug = { trace: true };

var trackingid = component.get("v.trackingid");
var userdata = component.get("v.userdata");

ga('create', trackingid, { 'userId': userdata.id, 'storage': 'none' });
ga('send', 'pageview');

Because you have access to the Salesforce "SecureWindow" I think this should work (I know it works with Locker Service turned on in regular Lightning)

For the full app, get it on github here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.