Launch: set a data element from within a rule?

morgant29080047

27-11-2018

How can I set a data element (I have it created as a custom code data element) from a rule? Basically I want the rule to fire when a user clicks a certain tile (there's a row of tiles with links). The following javascript figures out which tile it is and then tries to set the data element, but it's not working. tileIndex is correct, but the data element (featuredTileNumber) isn't getting set.

var tileIndex = $('.feature-tile-container .feature-tile .feature-tile-container__read-more-action .analytics-featuretitle-event').index(this)+1;

_satellite.logger.info('tile index= ' + tileIndex);

_satellite.setVar("featuredTileNumber",tileIndex);

_satellite.getVar("featuredTileNumber");

Accepted Solutions (1)

Accepted Solutions (1)

StewSchilling

MVP

04-12-2018

Are you saying that you have three ACTIONs in sequence?

Custom Code >> AA Set Variables >> AA Send Beacon

If so, then yes - it is a timing issue.  Custom Code runs asynchronously so the dynamic data element is not ready in time.

My suggestion (mentioned above) is slightly different.

In the CONDITIONs of your rule, create a Custom Code condition that sets your dynamic data element.  This will be sequentially executed (since condition MUST be evaluated first in order to determine if the rule actions are to be executed).

Then in your ACTIONs sequence, remove the custom code action, but keep the AA actions as-is.

Save, build, test, and report be in with your results.

Answers (12)

Answers (12)

morgant29080047

05-12-2018

Thank you so much! That was it and it's working now. Seriously, I appreciate the effort to help me out. Also learned that you can't use spaces in the Custom Link name field.

morgant29080047

04-12-2018

Thanks! I just read about not being able to setVar on Data elements. One odd requirement I should have added to the post was that the marketing person wants to me add the index # into the custom link name field. Essentially like this:

1639103_pastedImage_1.png

We have a property in DTM that's doing this and they want to re-create it in Launch the same way, but that's where the value is failing. I tried creating the data element on the fly as you suggested but it's not passing (which looks like it worked in DTM).

StewSchilling

MVP

03-12-2018

You seem to be doing everything correctly, so I will guess that you are missing just one thing...

Data elements that are created in the DTM or Launch UI cannot be set using the _satellite.setVar function.

_satellite.setVar can be used to create new data elements at runtime though.  I tend to use a special prefix to indicate that a data element is being created at run-time (such as "dyn_"). 

In your scenario, I would use a custom JS condition on the rule that sets the data element.  I just use this as an opportunity for code insertion and return true unconditionally at the end.  It's convenient because the click event (and 'this') are both in scope to be referenced.

Something like this:

Later you can reference this data element in the UI (perhaps in an AA set variables action) as %dyn_featuredTileNumber%

You can also reference it in custom code using _satellite.getVar("dyn_featuredTileNumber");

morgant29080047

04-12-2018

That's just what I'm calling the dynamic data element I created in the previous script.

_satellite.setVar("featuredTileNumber2",tileIndex.toString());

morgant29080047

04-12-2018

Thanks for the help but it's still not displaying - I'm just getting %featuredTileNumber2% writing out when the tag fires.

I wonder if it's my order of events somehow? I have a custom code block (where I have this js) -> set Analytics -> Set Beacon. Here's my console output:

   [Adobe Analytics] Firing link track beacon using the values: {"linkType":"o","linkName":"%featuredTileNumber2%"}.

   [Custom Script] featured content link rule fired

   [Custom Script] tile index= 1

StewSchilling

MVP

04-12-2018

Looks like you have an extra space in there.

Screen Shot 2018-12-04 at 1.34.32 PM.png

If, after removing the space, it still doesn't work, try removing the prefixed text "FeaturedContentTile: " to see if the data element can be resolved. If so, you might consider adding the prefix before doing the setVar.

adria_a94067176

04-12-2018

Don't do that, for the maintenance it's bad, any dataElement should get the info from the browser directly or from the DataLayer.

If you haven't a good datalayer try using the object smetrics. It's an object, so you can put some info inside like smetrics.thing = "stuff";

and in your dataelement write the following:

return (smetrics.stuff)? smetrics.stuff : "Ain't have a *hit";

Regards

Gigazelle

Employee

03-12-2018

A couple things you can try:

I know JS is pretty good about figuring out variable types, but you could try:

_satellite.setVar('featuredTileNumber',tileIndex.toString());

You also aren't telling the getVar to do anything. Typically it would be either be to a console output, or assigning it to another variable:

var tileNumber = _satellite.getVar('featuredTileNumber');

console.log(_satellite.getVar('featuredTileNumber'));