【问题标题】:GTM Custom Javascript Variable not working (return function)GTM 自定义 Javascript 变量不起作用(返回函数)
【发布时间】:2021-04-23 02:41:18
【问题描述】:

我已经看了好几天了,完全被卡住了。我正在使用的页面如下所示:

<div class="field--item">
<span class="file file--mime-application-pdf file--application-pdf icon-before">
<div class="file-info text-center--mobile"><span class="file-icon"><span class="icon glyphicon glyphicon-file text-primary" aria-hidden="true"></span></span><div class="file-wrapper"><span class="file-name">17840.pdf</span><span class="file-size">94.35 KB</span></div></div>
<span class="file-link"><a href="/dashboard/download/17840/122526" class="resource-button" resource-name="Kennedy's actions in Vietnam in 1962" resource-file-id="122526" resource-file-type="PDF" resource-subject="History" resource-category="">Download</a>
</span></span></div>

我设置的自定义变量如下所示:

function() {
    return document.getElementsByClassName('resource-button')[0].getAttribute('resource-name').text();
}

我真正想做的是设置一个变量,当单击“下载”按钮时,它会提取资源名称。我知道要单独设置实际的点击跟踪等,我只是无法让这个函数将任何东西拉到变量中,而不是“定义”。

非常感谢您对正确方向的任何帮助或点头。谢谢!

【问题讨论】:

    标签: javascript function return google-tag-manager click-tracking


    【解决方案1】:

    尝试删除最后的text() 函数:

    function getAttribute() {
      return document.getElementsByClassName('resource-button')[0].getAttribute('resource-name');
    }
    
    console.log(getAttribute());
    <div class="field--item">
    <span class="file file--mime-application-pdf file--application-pdf icon-before">
    <div class="file-info text-center--mobile"><span class="file-icon"><span class="icon glyphicon glyphicon-file text-primary" aria-hidden="true"></span></span><div class="file-wrapper"><span class="file-name">17840.pdf</span><span class="file-size">94.35 KB</span></div></div>
    <span class="file-link"><a href="/dashboard/download/17840/122526" class="resource-button" resource-name="Kennedy's actions in Vietnam in 1962" resource-file-id="122526" resource-file-type="PDF" resource-subject="History" resource-category="">Download</a>
    </span></span></div>

    【讨论】:

    • 哦,你绝对的钻石!!!这是一种享受,非常感谢你!!!!!!!
    【解决方案2】:

    也许你可以使用点击事件:

    <a href="/dashboard/download/17840/122526" onclick="myFunction(this);">Download</a>
    
    <script>
        function myFunction(elem) {
           var yourAttribute = elem.getAttribute('yourAttribute');
        }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2023-01-11
      • 1970-01-01
      • 2017-08-08
      • 2010-12-16
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 2019-05-03
      相关资源
      最近更新 更多