Expand my Community achievements bar.

SOLVED

Launch: set a data element from within a rule?

Avatar

Level 3

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");

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

14 Replies

Avatar

Employee Advisor

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'));

Avatar

Community Advisor

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");

Avatar

Level 2

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

Avatar

Level 2

by default smetrics doesn't exists in launch, it refers to window.s

Avatar

Level 3

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).

Avatar

Community Advisor

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.

Avatar

Level 3

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

Avatar

Community Advisor

Where is the “2” coming from ath end of featuredTileNumber2.  ??

Avatar

Level 3

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

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

Avatar

Correct answer by
Community Advisor

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.

Avatar

Level 2

This didn't work for me, when I added my code in the 'CONDITION', the rule didn't fire at all. My code checks if a button is clicked and sets the corresponding member in the xdm object. 

 

document.querySelector('#buy').addEventListener('click', function () {

    MyObject["clickAction"] = "set";

})

 

I don't see the rule being fired 

Avatar

Level 3

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.

Avatar

Community Advisor

Great to hear it!  Glad I could help. -Stew

TheMoreYouKnow.gif