I'm trying to call a Google Analytics event when an element with id=pricing is viewed. I cannot see a call being sent (checked via Network tab in Firebug).
我正在尝试在查看id = pricing的元素时调用Google Analytics事件。我看不到正在发送的呼叫(通过Firebug中的“网络”选项卡进行检查)。
My code looks as follows:
我的代码如下:
pricing_viewed = false;
function SendEvent() {
if (pricing_viewed === false) {
console.log("pricing viewed"); //this is properly shown in Console when the div with id=pricing is viewed
ga('send', 'event', 'Conversions', 'Viewed', 'Pricing table');
pricing_viewed = true;
}
}
$('#pricing').appear();
$('#pricing').on('appear', SendEvent);
I'm using Universal Analytics and GTM (although I'm trying to call the event directly from the code without GTM).
我正在使用Universal Analytics和GTM(虽然我试图直接从没有GTM的代码中调用事件)。
Any ideas on how to fix it?
关于如何修复它的任何想法?
1 个解决方案
#1
As Philipp Walten points out you have a syntax error.
正如Philipp Walten指出你有语法错误。
Your other/bigger problem is that GTM does not create the tracker with the default name (t0), so your send calls probably go nowhere. The solution would be to use a named tracker.
您的另一个/更大的问题是GTM没有使用默认名称(t0)创建跟踪器,因此您的发送呼叫可能无处可去。解决方案是使用命名跟踪器。
In GTM (assuming v2) you can go to your analytics tag, advanced configuration, check "set tracker name" and enter a name, e.g. "myTracker". Then change your inline code to
在GTM中(假设为v2),您可以转到分析标签,高级配置,选中“设置跟踪器名称”并输入名称,例如“myTracker”。然后将内联代码更改为
ga('myTracker.send', 'event', 'Conversions', 'Viewed', 'Pricing table');
That way you make sure all calls go the same tracker instance.
这样你就可以确保所有的调用都是同一个跟踪器实例。
Better yet use your sendEvent function to push data to the dataLayer variable, set up variables in GTM and use a second Analytics tag that's configured for event tracking.
更好的是使用sendEvent函数将数据推送到dataLayer变量,在GTM中设置变量并使用为事件跟踪配置的第二个Analytics标记。
#1
As Philipp Walten points out you have a syntax error.
正如Philipp Walten指出你有语法错误。
Your other/bigger problem is that GTM does not create the tracker with the default name (t0), so your send calls probably go nowhere. The solution would be to use a named tracker.
您的另一个/更大的问题是GTM没有使用默认名称(t0)创建跟踪器,因此您的发送呼叫可能无处可去。解决方案是使用命名跟踪器。
In GTM (assuming v2) you can go to your analytics tag, advanced configuration, check "set tracker name" and enter a name, e.g. "myTracker". Then change your inline code to
在GTM中(假设为v2),您可以转到分析标签,高级配置,选中“设置跟踪器名称”并输入名称,例如“myTracker”。然后将内联代码更改为
ga('myTracker.send', 'event', 'Conversions', 'Viewed', 'Pricing table');
That way you make sure all calls go the same tracker instance.
这样你就可以确保所有的调用都是同一个跟踪器实例。
Better yet use your sendEvent function to push data to the dataLayer variable, set up variables in GTM and use a second Analytics tag that's configured for event tracking.
更好的是使用sendEvent函数将数据推送到dataLayer变量,在GTM中设置变量并使用为事件跟踪配置的第二个Analytics标记。