Ziv Weissman
Ziv Weissman

Reputation: 4526

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?

Upvotes: 3

Views: 10701

Answers (1)

basarat
basarat

Reputation: 275867

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

@)-'--

Upvotes: 3

Related Questions