Dirk Diggler
Dirk Diggler

Reputation: 865

Omniture SiteCatlayst Tracking: Error when calling s.tl() from within a function with jQuery without binding to click event

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?

Upvotes: 0

Views: 4715

Answers (3)

Racheet
Racheet

Reputation: 740

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:

s.tl(true, 'o', 'Form Submitted');

Upvotes: 1

VaBeachKevin
VaBeachKevin

Reputation: 264

You could also try switching around the function setup.

function trackLink() {
    s=s_gi(s_account);
    s.trackExternalLinks = false;
    s.linkTrackVars = 'events,prop1';
    s.linkTrackEvents = s.events = 'event1';
    s.prop1 = s.pageName;
    s.tl(this, 'o', 'Form Submitted');
}

Upvotes: 0

Steve
Steve

Reputation: 55615

Try:

$.ajax({
    type: 'POST',
    url: submit.php,
    data: [data],
    success: trackLink.bind(this)
});

Upvotes: 0

Related Questions