1

Is there with javascript a way to retrieve the Google Analytics "Web Property ID" for a given GTM code?

I am including the snippet for GTM in the HEAD of my pages, and the Google Tag Assistant extension sees the tags properly, including the "Web Property Id" (UA-XXXXX-XX). I am using ReactGA module to send GA Events, and I need the "Web Property Id" to initialise it.

If I copy it to initialize the module like this;

ReactGA.initialize('UA-XXXXX-XX');

it is working properly, but I would prefer to retrieve it dynamically: i already map the GTM codes, i don't see any reason to do the same for the Web Property IDs if I can retrieve them from GTM codes.

2 Answers2

3

Google Analytics is storing information about ID in global variable gaData, you can receive GA ID information like that:

Object.keys(gaData)[0]
//it will return string UA-XXXXXXX
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36
  • Thanks, it works! I've also found this that returns the same id: `ga.getAll()[0].get('trackingId')`. Now I have make sure that "ga" and "gaData" are defined before using them. – nevermind777 Jul 07 '17 at 07:24
0

I would assume this is not possible. The reason being that the property ID would most likely be stored in the GTM container as a constant variable. Client side (i.e. via JavaScript), you would have no way of knowing the name (or unique ID) of this GTM variable. There is a JavaScript function available:

google_tag_manager['GTM-XXXXXX'].macro(i)

This macro function accepts a single integer as a parameter. This integer represents the "id" of the GTM variable. As I said above, you have no way of knowing what the ID of the (constant) variable that stores your GA property ID is, so this function is of no use to you.

faridghar
  • 1,445
  • 2
  • 13
  • 25