如何使用JQuery查找Tag Manager容器ID

时间:2021-12-20 15:15:24

I need to find what the Google Tag Manager container ID is after the page loads.

我需要在页面加载后找到Google跟踪代码管理器容器ID的内容。

I cannot get it from the same source as i'm adding it to the page.

我无法从相同的来源获取它,因为我将它添加到页面。

i want to set a jQuery variable to GTM-XXXXXX

我想将一个jQuery变量设置为GTM-XXXXXX

i have been messing with

我一直在搞乱

var scripts = $("script");

but this is returning an array, and these are subject to an order change so i don't think it is too reliable

但是这会返回一个数组,这些都会受到订单更改的影响,因此我认为它不太可靠

UPDATE 1:

am using the following to successfully return container ID, it just feels way to flaky and wrong

我正在使用以下方法成功返回容器ID,它只是让人感觉到片状和错误

var scripts = $("script");

var scripts = $(“script”);

if (scripts.length) {
  console.log(scripts[2].src.substring(42, 52));
}

2 个解决方案

#1


2  

This is untested, but you could maybe look for something better about the script that identifies the tag manager, such as part of the URL. Let's get the full source URL for the script that loads:

这是未经测试的,但您可以寻找更好的标识标记管理器的脚本,例如URL的一部分。让我们获取加载脚本的完整源URL:

var tagManagerUrl = $('script[src*="googletagmanager"]').attr('src');

Now you have the URL whose query string contains the ID, but we need a way to parse the query string value from it. Let's take a look at the code from this answer, which gets a query string value by name from the current location.

现在您拥有其查询字符串包含ID的URL,但我们需要一种方法来解析查询字符串值。让我们看看这个答案中的代码,它从当前位置按名称获取查询字符串值。

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

It's close, but we have to modify it to allow us to pass in our own query string:

它很接近,但我们必须修改它以允许我们传入我们自己的查询字符串:

function getParameterByName(qs, name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(qs);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

Now we can use it to find the id value, which should be your container ID:

现在我们可以用它来查找id值,它应该是你的容器ID:

var tagManagerContainerId = getParameterByName(tagManagerUrl.split('?')[1], 'id');
// do something with it

You will need to run this after the tag manager itself loads.

您需要在标记管理器本身加载后运行它。

#2


0  

While Cory's answer is great I don't think you need jQuery at all, since in most scenarios it would even be simpler to use the container id variable (needs to be enabled in the variables section of the interface) in GTM and run the javascript function from inside a custom HTML tag:

虽然Cory的答案很棒,但我认为你根本不需要jQuery,因为在大多数情况下,在GTM中使用容器id变量(需要在界面的变量部分中启用)甚至更简单并且运行javascript从自定义HTML标记内部运行:

<script>
console.log({{Container ID}});
</script>

(although I admit that does not answer the question of how to do this with jQuery).

(虽然我承认没有回答如何使用jQuery执行此操作的问题)。

#1


2  

This is untested, but you could maybe look for something better about the script that identifies the tag manager, such as part of the URL. Let's get the full source URL for the script that loads:

这是未经测试的,但您可以寻找更好的标识标记管理器的脚本,例如URL的一部分。让我们获取加载脚本的完整源URL:

var tagManagerUrl = $('script[src*="googletagmanager"]').attr('src');

Now you have the URL whose query string contains the ID, but we need a way to parse the query string value from it. Let's take a look at the code from this answer, which gets a query string value by name from the current location.

现在您拥有其查询字符串包含ID的URL,但我们需要一种方法来解析查询字符串值。让我们看看这个答案中的代码,它从当前位置按名称获取查询字符串值。

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

It's close, but we have to modify it to allow us to pass in our own query string:

它很接近,但我们必须修改它以允许我们传入我们自己的查询字符串:

function getParameterByName(qs, name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(qs);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

Now we can use it to find the id value, which should be your container ID:

现在我们可以用它来查找id值,它应该是你的容器ID:

var tagManagerContainerId = getParameterByName(tagManagerUrl.split('?')[1], 'id');
// do something with it

You will need to run this after the tag manager itself loads.

您需要在标记管理器本身加载后运行它。

#2


0  

While Cory's answer is great I don't think you need jQuery at all, since in most scenarios it would even be simpler to use the container id variable (needs to be enabled in the variables section of the interface) in GTM and run the javascript function from inside a custom HTML tag:

虽然Cory的答案很棒,但我认为你根本不需要jQuery,因为在大多数情况下,在GTM中使用容器id变量(需要在界面的变量部分中启用)甚至更简单并且运行javascript从自定义HTML标记内部运行:

<script>
console.log({{Container ID}});
</script>

(although I admit that does not answer the question of how to do this with jQuery).

(虽然我承认没有回答如何使用jQuery执行此操作的问题)。