I'm fairly new to Salesforce and Lightning Components, so please bear with me. I have a test Lightning Component named ajAppComponent
and want to embed Google Analytics in the component.
First, I added https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-1 as a static resource named GoogleAnalytics
. I've also added https://www.google-analytics.com/analytics.js as analytics
.
In my ajAppComponent.cmp
file, I've added the following line:
<ltng:require scripts="{!'/resource/GoogleAnalytics'}" afterScriptsLoaded="{!c.gaLoaded}" />
I've also tried it loading the analytics
static resource, but that didn't help either.
In my ajAppComponentController.js
I have the gaLoaded
function, which has:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-1');
But neither show any activity in my Google Analytics homepage. I know it's loading the gaLoaded
function as I've put in debugging code (console.log) and I see the resource loaded when I look in Google Dev Tools.
Am I missing a step somewhere? Any help would be greatly appreciated!