Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use google analytics in typescript and access dataLayer

I am trying to access GA's dataLayer in typescript code. (angular controller).

I want to achieve this 'normal' js code:

dataLayer.push({
  'event': 'LoginSuccess'
});

What I tried to do: (I've added the google analytics d.ts)

declare var dataLayer: GoogleAnalyticsCode;


//This fails:    
dataLayer.push({
  'event' : 'LoginSuccess'
});

//This is OK with typescript but I'm not sure this is how it suppose to be in GA:
dataLayer.push(['event', 'LoginSuccess']);

If i declare the dataLayer as Array the push works fine as the original js code...

Is this how it should be achieved? or what is the best practice?

like image 851
Ziv Weissman Avatar asked Oct 18 '25 01:10

Ziv Weissman


1 Answers

Reading the docs (https://developers.google.com/tag-manager/devguide?hl=en) the following seems the right way

dataLayer.push({
  'event' : 'LoginSuccess'
});

This is a bug in the definition file. A PR with clear reasoning and further analysis would be appreciated

@)-'--

like image 195
basarat Avatar answered Oct 19 '25 13:10

basarat