gtag 代码段中的参数变量是什么?

What is the arguments variable in the gtag snippet?

未决问题。查看全局站点代码 (gtag.js) 片段,我不太了解 arguments 变量的用法。

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_TRACKING_ID');
</script>

具体是什么意思?这里的dataLayer.push(arguments)有什么用?

谢谢!

对于 Analytics 或 gtag 来说,这没什么特别的。它只是一个标准的 Javascript 对象。它代表传递给函数的所有参数。

analytics 在这里所做的只是将一个对象推入 dataLayer,并将所有参数传递给 gtag 标签。这并不意味着您可以简单地删除 gtag 函数并直接使用 dataLayer,因为一旦加载 gtag.js 文件,它就可以用保持相同界面的不同函数替换 gtag 函数。

发件人:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

The arguments object is a local variable available within all (non-arrow) functions. You can refer to a function's arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0. For example, if a function is passed three arguments, you can refer to them as follows:

arguments[0]
arguments[1]
arguments[2]