function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vipin PVipin P 

Google Analytics in Lightning component

We are trying to put GA code inside lightining  component. I added the analytics js as static resource:
<ltng:require scripts="/resource/ga/analytics.js"  afterScriptsLoaded="{!c.loadGoogleAnalytics}" />
and in the controller I used this satic resource ( /resource/ga/analytics.js ) :
(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','/resource/ga/analytics.js','ga');
For some reason the last line ( (window,document,'script','/resource/ga/analytics.js','ga'); )  is not working I get the following Error :
[Cannot read property 'insertBefore' of undefined]
I tried with the actaul URL ( //www.google-analytics.com/analytics.js ) as well but throws the same error.
(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');


Any one have any idea on how to fix it ?





 
Vipin PVipin P
I did also try this:
var analyticsUrl = $A.get('$Resource.ga') + '/analytics.js';
          
        (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',analyticsUrl,'ga');

but no luck
Vipin PVipin P
I just noticed the isse is when the 'Lightning Locker Service Security’ is enabled. If it deactivated it works. Any one expericed this issue ?