Omniture SiteCatlayst 跟踪:使用 jQuery 从函数内调用 s.tl() 而不绑定到单击事件时出错
我想使用 Omniture 的“自定义链接跟踪”来跟踪用户何时提交表单。此功能利用内置函数s.tl()
。典型的设置如下所示:
$('a#submit').click(function () {
s.trackExternalLinks = false;
s.linkTrackVars = 'events,prop1';
s.linkTrackEvents = s.events = 'event1';
s.prop1 = s.pageName;
s.tl(this, 'o', 'Form Submitted');
});
单击示例链接 () 时,此代码可以正常工作。相反,我们想要调用一个函数来触发链接跟踪:
// function to track custom link
var trackLink = function() {
s.trackExternalLinks = false;
s.linkTrackVars = 'events,prop1';
s.linkTrackEvents = s.events = 'event1';
s.prop1 = s.pageName;
s.tl(this, 'o', 'Form Submitted');
};
// Form Submission code calls trackLink()
$.ajax({
type: 'POST',
url: submit.php,
data: [data],
success: trackLink()
});
调用 trackLink()
会导致 undefined
,大概是因为 this
在s.tl()
不再指向 DOM 对象?将 this
替换为 $('a#submit')[0]
之类的内容(尝试将对象传递给它而不是 this
)结果未定义
。我做错了什么?
I want to track when a user submits a form using Omniture's "Custom Link Tracking". This feature utilizes the built-in function s.tl()
. A typical setup looks like this:
$('a#submit').click(function () {
s.trackExternalLinks = false;
s.linkTrackVars = 'events,prop1';
s.linkTrackEvents = s.events = 'event1';
s.prop1 = s.pageName;
s.tl(this, 'o', 'Form Submitted');
});
This code works fine when the example link (<a id="submit">
) is clicked. Say, instead, we want to call a function to trigger the link tracking:
// function to track custom link
var trackLink = function() {
s.trackExternalLinks = false;
s.linkTrackVars = 'events,prop1';
s.linkTrackEvents = s.events = 'event1';
s.prop1 = s.pageName;
s.tl(this, 'o', 'Form Submitted');
};
// Form Submission code calls trackLink()
$.ajax({
type: 'POST',
url: submit.php,
data: [data],
success: trackLink()
});
Calling trackLink()
results in undefined
, presumably because the this
in s.tl()
no longer points to a DOM object? Replacing this
with something like $('a#submit')[0]
(trying to pass an object to it instead of this
) also results in undefined
. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
s.tl 的第一个参数只能有两个值之一。
如果该函数作为元素的 onclick 处理程序被调用,则它采用值 - this -,在本例中解析为元素的 href 属性的值。如果将此作为第一个参数传递,则该函数将在加载新页面之前创建 500 毫秒的延迟,这是为了确保有足够的时间发送跟踪调用。
如果在任何其他上下文中调用该函数,包括作为表单或 ajax 成功处理程序的一部分,则第一个参数必须是文字 - true -。在这种情况下,该函数不会添加延迟,但仍会发送跟踪调用。
在你的情况下,正确的签名是这样的:
The first parameter for s.tl can only have one of two values.
If the function is being called as the onclick handler for an element, then it takes the value - this -, which in this case resolves to the value of the href attribute of the element. If this is passed as the first parameter, then the function will create a 500ms delay before the new page is loaded, this is to ensure there is enough time for the tracking call to be sent.
If the function is being called in any other context, including as part of a form or ajax success handler, then the first parameter has to be a literal - true -. In this case, the function doesn't add the delay, but will still send the tracking call.
In your case the correct signature is this:
尝试:
Try:
您也可以尝试切换功能设置。
You could also try switching around the function setup.