Expand my Community achievements bar.

SOLVED

Capture Anchor Text and Href in Launch Code

Avatar

Level 4

Hello,

I'm trying to capture the an anchor link's text and href and pass them into an evar.  I've successfully created the event to capture when a visitor clicks a link, however I'm having trouble setting the values in the Analytics call.  The s.linkType and s.linkName fire correctly but my props, events and evars are not applied to the s.tl() request.

Rule

click-tracking.jpg

Set Launch Var Code

var turl = this.href.toLowerCase();

var ttext = this.text.trim().toLowerCase();

var tsection = 'body';

s.linkTrackEvents = "event15";

s.linkTrackVars = "prop6,eVar16";

s.eVar16 = s.prop6 = [tsection, ttext, turl].join('|');

s.events = "event15";

console.log(s.eVar16);

The console shows that s.eVar16 contains the value I want to see (e.g., 'body|add to cart|/shop/parts.html') but when I look in the Experience Cloud Debugger none of the props, evars or events have been assigned to the link click.

What am I missing?

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hey MktgCloudUser,

I'm going to guess a bold assumption here, but I think it's likely the issue: "Is that a Core - Custom Code Action Block?"  If it is, then hooray we've found the issue!  If not, just ignore this long post

Unfortunately, using a custom code action in this manner will not work for the population of values on click events (or any trigger that is not page top or page bottom - since that code is provided with-library).  Adobe launch does not wait for any response or completion of custom code actions, so you've got a race condition going on (which always loses, since Postscribe is pretty inefficient compared to other activities in this chain).  It simply writes the code to be executed and then moves on to the next action right away, before the code actually executes.

So right now, here's your Rule Action order:

1. Clear Variables

2. Begin postscribe for your custom code (starts writing your non-minified custom code block into the DOM!)

3. Send Beacon runs (this is where your LinkType, LinkName, LinkURL is being populated from)

4. Your postscribed custom code is finally getting around to executing, which is too late to be included in the send beacon (it's already gone)...  Also, you clear vars first everytime too, so if you try to fire the rule again it'll clear out and postscribe again, so you'll get no custom variables each and every time a beacon is sent.

The main reason this cannot work within Launch is that the Action of executing Custom Code is never done in a controlled synchronous manner unless included in library (Launch doesn't know that your code needs to return something, nor does it get a callback when it completes).  If you want to set variables and still be within the controlled order of Adobe Launch operations, you need to use something that Launch waits for.

I'd recommend one of the following approaches instead (both of these are examples where Adobe Launch waits for a response before continuing to further actions.

Option 1: Using the Action Node from Analytics:  Analytics Set Variables

Whether you use a data element within here, or the custom code section at the bottom, Adobe Launch knows it needs to hold off every further Analytics Extension Action until it completes, simply because it's an Analytics Set Variables type node.  This will allow you time to complete all your code and set the Vars before the beacon call action happens.

Option 2: Using the Conditions area of the Rule, and then making it return true

This one works in the same way, whether you engage a data element, or use custom code directly it will still work because Adobe Launch knows it needs to wait for a response before letting other Actions or Rules fire.  You will have to adjust how you clearVars in order to do this - since this would happen before the actions.

Hopefully this helps answer the questions... and I sure hope I guessed right about that Custom Code action block.

View solution in original post

8 Replies

Avatar

Level 9

Could it be that you overwrite s.linkTrackVars and s.linkTrackEvents in your doPlugins method?

I was also struggling with these two variables and added some helper method to concatenate the string:

function concatenate(value, addition) {

    if (typeof value === 'string' && value !== '') {

        value += ',' + addition;

    } else {

        value = addition;

    }

    return value;

}

then I call it like this in my rule:

s.linkTrackVars = concatenate(s.linkTrackVars, 'events,eVar11,prop11');

s.linkTrackEvents = concatenate(s.linkTrackEvents, 'event4');

s.events = concatenate(s.events, 'event4');

Also, you are missing "events" in your "linkTrackVars".

Avatar

Level 4

Frustrating...  My original code showed the evar's contents in the console so I know they were populated but I've updated my code to include the missing event and to concatenate the values.  I tried your script as well as Adobe's appendList plugin and neither works.  Tried on multiple browsers and the link_click request never contains the evars/events when it fires and there aren't any errors in the console.

Console (using console.log(s.eVar16);)

click-tracking console.jpg

EC Debugger

missing click tracking.jpg

Updated Code

function concatenate(value, addition) {

    if (typeof value === 'string' && value !== '') {

        value += ',' + addition;

    } else {

        value = addition;

    }

    return value;

}

var turl = this.href.toLowerCase();

var ttext = this.text.trim().toLowerCase();

var tsection = 'body';

s.eVar16 = s.prop6 = [tsection, ttext, turl].join('|');

s.events = "event15";

s.linkTrackVars = concatenate(s.linkTrackVars, 'events,eVar16,prop6');

s.linkTrackEvents = concatenate(s.linkTrackEvents, 'event15');

s.events = concatenate(s.events, 'event15');

console.log(s.eVar16);

Avatar

Level 9

Can you check your doPlugins method as well? Maybe you accidently overwrite the s variables within this method. It will get executed just before sendBeacon(), so after your setVariables action.

Avatar

Level 4

My doPlugins in part of the Analytics extension's Configure Tracker Using Custom Code section.  I completely removed everything in the editor (including all comments) and attempted again without success.

Avatar

Level 9

Sorry, I am also running out of ideas what could go wrong on your side. I still assume that there is something either deleting the eVars and props before sendBeacon or that s.linkTrackVars does not contain what you expect it to contain.

I think the only way to continue is actually debugging the JS. Load the unminified version of your launch library add a breakpoint in your setVariable action and step through it untiil the sendBeacon...

Avatar

Community Advisor

If you are still struggling with this, here are a couple of things that you could try:

1) Add console.log statements for s.eVar16 and s.linkTrackVars in your rule's AA custom code AND in the AA extension's custom code doPlugins function.  i.e.

2) Do a "search all files" from your Chrome dev tools to see if you are overwriting s.linkTrackVars somewhere.

3) Just for giggles, try adding this in a custom condition of your Rule:

Then in the UI of your Analytics Extension, Set Variables action, set prop6 to %l_linkInfo% and do the same for eVar16.

-Stew

Avatar

Correct answer by
Level 4

Hey MktgCloudUser,

I'm going to guess a bold assumption here, but I think it's likely the issue: "Is that a Core - Custom Code Action Block?"  If it is, then hooray we've found the issue!  If not, just ignore this long post

Unfortunately, using a custom code action in this manner will not work for the population of values on click events (or any trigger that is not page top or page bottom - since that code is provided with-library).  Adobe launch does not wait for any response or completion of custom code actions, so you've got a race condition going on (which always loses, since Postscribe is pretty inefficient compared to other activities in this chain).  It simply writes the code to be executed and then moves on to the next action right away, before the code actually executes.

So right now, here's your Rule Action order:

1. Clear Variables

2. Begin postscribe for your custom code (starts writing your non-minified custom code block into the DOM!)

3. Send Beacon runs (this is where your LinkType, LinkName, LinkURL is being populated from)

4. Your postscribed custom code is finally getting around to executing, which is too late to be included in the send beacon (it's already gone)...  Also, you clear vars first everytime too, so if you try to fire the rule again it'll clear out and postscribe again, so you'll get no custom variables each and every time a beacon is sent.

The main reason this cannot work within Launch is that the Action of executing Custom Code is never done in a controlled synchronous manner unless included in library (Launch doesn't know that your code needs to return something, nor does it get a callback when it completes).  If you want to set variables and still be within the controlled order of Adobe Launch operations, you need to use something that Launch waits for.

I'd recommend one of the following approaches instead (both of these are examples where Adobe Launch waits for a response before continuing to further actions.

Option 1: Using the Action Node from Analytics:  Analytics Set Variables

Whether you use a data element within here, or the custom code section at the bottom, Adobe Launch knows it needs to hold off every further Analytics Extension Action until it completes, simply because it's an Analytics Set Variables type node.  This will allow you time to complete all your code and set the Vars before the beacon call action happens.

Option 2: Using the Conditions area of the Rule, and then making it return true

This one works in the same way, whether you engage a data element, or use custom code directly it will still work because Adobe Launch knows it needs to wait for a response before letting other Actions or Rules fire.  You will have to adjust how you clearVars in order to do this - since this would happen before the actions.

Hopefully this helps answer the questions... and I sure hope I guessed right about that Custom Code action block.

Avatar

Level 4

Hello Everyone, thanks for the input.  I think gflare​ probably got it right but I had a deadline well before his post and ended up manually executing the custom link request right after my code.  So in Actions, I removed the Analytics extension's Set Variables and Send Beacon and only have:

Clear Variables > Core Custom Code

In the custom code (JavaScript), I just manually make the s.tl request right after the code I already had:

s.linkTrackVars = "events,eVar16,prop6";

s.linkTrackEvents = "event15";

s.eVar16 = s.prop6 = [tsection, ttext, turl, tqstring].join('|');

s.tl(this,'o', 'from ' + _satellite.getVar('Page: Channel') + ' channel link click');

Thanks for helping me troubleshoot.