如何从GTM代码获取GA跟踪ID

时间:2021-12-26 14:34:17

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

是否有javascript方法可以检索给定GTM代码的Google Analytics“网络媒体资源ID”?

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.

我在我的页面的HEAD中包含了GTM的片段,Google Tag Assistant扩展程序正确地看到了标签,包括“Web Property Id”(UA-XXXXX-XX)。我正在使用ReactGA模块发送GA事件,我需要“Web Property Id”来初始化它。

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.

它工作正常,但我更愿意动态检索它:我已经映射了GTM代码,如果我可以从GTM代码中检索它,我认为没有任何理由对Web属性ID做同样的事情。

2 个解决方案

#1


2  

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

Google Analytics正在将有关ID的信息存储在全局变量gaData中,您可以接收以下GA ID信息:

Object.keys(gaData)[0]
//it will return string UA-XXXXXXX

#2


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:

我认为这是不可能的。原因是属性ID很可能作为常量变量存储在GTM容器中。客户端(即通过JavaScript),您将无法知道此GTM变量的名称(或唯一ID)。有一个JavaScript函数可用:

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

google_tag_manager [ 'GTM-XXXXXX']。宏(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.

此宏函数接受单个整数作为参数。该整数表示GTM变量的“id”。如上所述,您无法知道存储GA属性ID的(常量)变量的ID是什么,因此此功能对您没用。

#1


2  

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

Google Analytics正在将有关ID的信息存储在全局变量gaData中,您可以接收以下GA ID信息:

Object.keys(gaData)[0]
//it will return string UA-XXXXXXX

#2


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:

我认为这是不可能的。原因是属性ID很可能作为常量变量存储在GTM容器中。客户端(即通过JavaScript),您将无法知道此GTM变量的名称(或唯一ID)。有一个JavaScript函数可用:

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

google_tag_manager [ 'GTM-XXXXXX']。宏(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.

此宏函数接受单个整数作为参数。该整数表示GTM变量的“id”。如上所述,您无法知道存储GA属性ID的(常量)变量的ID是什么,因此此功能对您没用。