在动态加载的脚本中访问Google ga var

access google ga var in dynamically loaded script

本文关键字:访问 Google ga var 脚本 动态 加载      更新时间:2023-09-26

我已经动态加载了html和js, html包含一个按钮链接(使用Smarty但不相关的问题):

<tr>
    ...
    <td><button id="rid_{$roomRef}" type="button" class="btn btn-success" href="{$link}">&pound;{$rate|string_format:"%0.2f"}</button></td>
</tr>

每个tr也带来自己的js:

$('#rid_{$roomRef}').click(function() {
    window.location.href = $(this).attr('href');
});

我想做的是添加一个谷歌分析跟踪链接,像这样:

ga(‘send’, ‘event’, ‘Supplier’, ‘Click’, $(this).attr('href'));

就在窗口事件之前,但我在控制台中得到一个错误:

Uncaught SyntaxError: 
Unexpected token ILLEGAL jquery-2.1.1.min.js:2
n.extend.globalEval jquery-2.1.1.min.js:2
n.fn.extend.domManip jquery-2.1.1.min.js:3
n.fn.extend.append jquery-2.1.1.min.js:3
(anonymous function) jquery-2.1.1.min.js:3
n.access jquery-2.1.1.min.js:2
n.fn.extend.html jquery-2.1.1.min.js:3
$.ajax.success (index):306j jquery-2.1.1.min.js:2
k.fireWith jquery-2.1.1.min.js:2
x jquery-2.1.1.min.js:4
(anonymous function) jquery-2.1.1.min.js:4

你知道这里发生了什么吗?

您的GA代码有大引号。注意send/event周围的引号与$(this).attr('href')

之间的区别

ga("发送"、"事件"、"供应商","点击",(这).attr美元("href")),