How to use google analytics in typescript and access dataLayer

Ziv Weissman picture Ziv Weissman · Sep 10, 2015 · Viewed 7.4k times · Source

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?

Answer

basarat picture basarat · Sep 11, 2015

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

@)-'--