7

It sounds like a patch was released that allows us to create a lightning component that can load up google analytics javascript.

https://www.lightningdesignsystem.com/release-notes/

Screenshot from https://success.salesforce.com/_ui/core/chatter/groups/GroupProfilePage?g=0F9300000001oDdCAI&fId=0D53000002I99WnCAJ

Has anyone had any luck doing this? It sounds like I just need to build a client-side controller that loads up a function with the Google Analytics code function.

Here is what I am trying:

Component:

    <ltng:require scripts="/resource/ga.js"
                  afterScriptsLoaded="{!c.loadAnalytics}"/>
</aura:component>

Client Controller:

({
    loadAnalytics : function(component, event, helper) {
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                                })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

        ga('create', '***API Token***', 'auto');
        ga('send', 'pageview');

    }
})

1 Answer 1

2

As of Winter '17 Lightning communities (Napili Template) lets you enable this functionality OOTB, you just need your tracking ID and you can indicate if you authorize Google Analytics to access additional data (queries, user types and activities) enter image description here

Track, Learn, and Fine-Tune with Google Analytics

Enable page tracking by adding your Google Analytics™ tracking ID in Community Builder. Use the resulting data to learn more about the pages your customers visit. Then adjust your community’s pages to more accurately reach your customers. After you enter your Google Analytics ID in Settings | Advanced, publish a community to enable the service. Your community’s analytics data streams directly to the Google Analytics service where you can review and interpret it.

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.