Unable to get current click value through Adobe Launch

2.2k Views Asked by At

I created a click event in adobe launch which will capture the value of the link and send it to analytics. I have created a data element for saving the value and in my DOM I am saving value in local storage.

Local storage code:

$('.card').click(function() {
       var name = $(this).text();
       localStorage.setItem('productName', name);
     });

My problem is when I click the first link no value got saved, but after when I click second link it saves the value of first link and on third link saves value of second link and so on. I want to save current link value in evar3 variable.

Data element:

Data element

Rule: Rule

Set variables: Set Variables

Thanks, Harshit

2

There are 2 best solutions below

0
On

Rather than defining it in Data Element I would say its better to keep it directly in the Rule itself. Please try with this %this.@text%.
Let me know if this helped.

5
On

I'm scratching my head a little bit about why your jQuery selector doesn't match your Rule selector, but that's probably not immediately related or relevant, considering you said you are seeing data pop, in general, so at face value, I'm going to ignore that.

But in general, it sounds like your jQuery code is getting executed after the Rule is evaluated, so it's effectively one step behind. I'm not sure there's much if anything you can do about that if you aim to keep two separate click event listeners like this.

You're going to have to restructure to have one chain off the other (which I don't think is actually feasible with jQuery > Launch as-is. Maybe if you write two separate custom code blocks with promise chaining but that kind of sidesteps Launch and IMO is over-complicating things to begin with (see below)). Better yet, merge them into a single click event listener. On that note..

Why do you have two separate click event listeners? Is the sole reason for this to pass off the link text? Because you can reference the clicked element in the Launch Rule itself.

You should be able to reference %this.innerText% in the Set Variables fields. Or you can reference this object in custom code boxes within the Rule.

IOW at face value I don't see why you need or should be creating/using that jQuery listener or pushing to local storage like that.

Update:

You commented the following:

I tried with %this.innerText% it is also not showing current values. I am pushing the value first to local storage because my link values are generating on runtime through an API. They are not hardcoded. I am also trying to figure out why my rule is getting fired before my jquery is evaluated. But when I check in console using _satellite.getVar('Product Name'); it shows me the correct value, but in debugger console value is wrong. Can you show me the way you want to create rule to getting it fired correctly ? Thanks,

Okay so your link values are generated runtime through an API call? Well okay now that sounds like where the (timing) issue is, and it's the same issue in principle I mentioned you prolly had between the jQuery and Launch code. Assuming you can't change this dynamic link functionality, you have two options:

1. Explicitly trigger a launch rule (direct call rule) in a callback from the API code that's generating the link values.

This is the better method, because you won't have race condition issues with your API vs. link tracking code. The biggest caveat about this method though is that it requires requires you to actively add code to the site, which may or may not be feasible for you.

I have no idea what your code for generating the link looks like, but presumably it's some ajax call and generated from the success callback. So in the callback, you'd add a line of code something like this:

_satellite.track('product_image_click', {
    text : elem.innerText
});

Again, I don't know what your API code that generates the link looks like, but presumably you have within it some element object you append to or update the DOM, so for this example, I will refer to that as elem.

'product_image_click' - This is the value you use for the direct call rule identifier in the interface, e.g. :

enter image description here

And then _satellite.track() call also includes an object payload in 2nd argument that you can pass to the direct call rule. So in the code above, I set a property named text and give it a value of elem.innerText.

Then, within the direct call rule, where you set variables, the data you passed can be referenced from event.details object within custom code box (e.g. event.details.text), or using % syntax within the interface fields (e.g. %event.details.text%).

enter image description here

2. Make use of setTimeout to delay evaluating the link click.

The one advantage of this method over option #1 is that it is passive. You don't have to add code directly to your site to make it work.

However, this is the shadier option, because you don't really know how long it will take for your link to be generated. But generally speaking, if you can determine it takes on average say 250ms for the link generation API to do its thing, and you set the timeout to be called at say 300-500ms, then you will probably be okay most of the time. However, it's never a 100% guarantee.

In addition, if clicking on the link ultimately redirects the visitor to another page, then this solution will likely not work for you at all, since the browser will almost certainly have navigated before this has a chance to execute. Because of this, I normally I wouldn't even mention this as an option, but since you mentioned this link involves an API that generates the link values when clicked, I figured maybe this isn't a navigation / redirect link, so maybe this is an option you can go for, if you can't do option #1.

First, create a direct call rule the same as shown in option #1. This will be the rule that receives the link text and makes the Adobe Analytics (or whatever other marketing tag) calls.

Then, create a separate rule as a click event, similar to what you are currently trying to do (listening for the link click, based on your css selector). In this rule, you won't be setting any AA variables. Instead, add a custom js box with code like this:

var elem = this;
(function (elem) {
    window.setTimeout(function() {
        _satellite.track('product_image_click', {
            text : elem.innerText
        });
    }, 500);
})(elem);

So when the rule is triggered, it will create a setTimeout callback to call the first rule, passing the link text in the payload. And.. hopefully the 500ms timeout shown in the code example is enough time for the API to do its thing. You may be able to adjust it up or down.