Reputation: 135
I'm trying to fire some tracking info on link clicks and I've been trying out functions I put together from different places I found online.
After some testing I ended up with two functions that fired what I wanted. However, these seem to be writing to global object, since after the function is called the event keeps firing in other places. I got around this by adding the last line, but this seems like a band-aid that might be covering up a potentially bigger issue. What am I doing wrong in the first place, and what should I be doing to avoid this? I couldn't get the events to fire without writing to the s.events object
function f1(){
s.linkTrackVars= s.linkTrackVars+',eVar45,events';
s.eVar45='Chat button exposed';
s.linkTrackEvents = s.events = 'event11'; // hmmm
s.tl(this, 'o', 'blah');
s.linkTrackVars = s.linkTrackEvents = s.events = ""; // Added this to empty the global objects
}
function f2(){
s.linkTrackVars= s.linkTrackVars+',eVar46,prop45,events';
s.eVar46='Clicked chat button';
s.prop45='Clicked chat button';
s.linkTrackEvents = s.events = 'event31'; // hmmm
s.tl(this, 'o', 'blah');
s.linkTrackVars = s.linkTrackEvents = s.events = ""; // Added this to empty the global objects
}
I'm guessing the issue is the line marked "hmm" but that was the only way I could get the event to fire, if I remove either s.linkTrackEvents or s.events it doesn't fire the event. Either way it seems I have to set all variables to empty strings to avoid the values being used by other actions. Any ideas?
Thanks
Upvotes: 1
Views: 451
Reputation: 32517
Firstly, when you want to send variables to Adobe Analytics via an s.tl()
call, yes, they must be "registered" with linkTrackEvents
and linkTrackVars
.
However, you can pass the variables in an object as the 4th argument to use the values for that call but not permanently set them. You still need to "register" the vars with linkTrackEvents
and linkTrackVars
so that needs to go in the payload, too.
Example
function f1() {
var payload={
'linkTrackEvents':'event1',
'linkTrackVars':'events,eVar1',
'events':'event1',
'eVar1':'bar'
}
s.tl(true,'o','some action',payload)
}
/*
first we have the var set, maybe as default values
in some config initially loaded on page
*/
s.linkTrackEvents='None';
s.linkTrackVars='None';
s.events='';
s.eVar1='foo';
// example to see current values above
console.log(s);
/*
then call the function. You will see in the
request that event1 is set and eVar1 is 'bar'
*/
f1();
/*
now another console log to see the values,
you will see it's the original values
*/
console.log(s);
Upvotes: 3