Can you manually set Analytics events/eVars in SDK event Callback | Community
Skip to main content
isaakskinner
Level 2
August 2, 2023
Solved

Can you manually set Analytics events/eVars in SDK event Callback

  • August 2, 2023
  • 3 replies
  • 4295 views

I was trying to see if it was possible to update the XDM object using the SDK On before event send callback.

I attempted to write the XDM Object with the following code.

Essentially, trying to create some conditional logic to populate eVars in specific instances. 

 

On before event send callback

 

if(digitalData.event.linkClick.href){
content.xdm._experience.analytics.customDimensions.eVars.eVar16 = digitalData.event.linkClick.href;
}

 

Testing in the Network call, i see the customerDimensions.eVars.eVar16 populated, but in the Console, I see an error saying the .analytics objects can't be accessed. 

 

It's likely I am missing something, but wanted to see if I was doing anything that was against what is possible/recomneded.

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by leocwlau

The problem is on the left-hand side of the assignment statement, content.xdm._experience.analytics.customDimensions.eVars.eVar16. You can try to print the "content" out in the console as @yuhuisg suggested and you will find there is no "analytics" sub-object under "_experience". The "content" you have in the callback contains only the XDM passing into the sendEvent call instead of a full scheme structure.

It is a practice, when doing value assignments, to check path existence and construct any necessary sub-object leading to the attribute you are assigning.

3 replies

yuhuisg
Community Advisor
Community Advisor
August 3, 2023

Your code looks correct. I've used the on before event send callback too, though not with the "_experience" field group, but I've never encountered this error.

Perhaps you can add a "console.log" in your code to log your content.xdm object at the time that the callback code is run. That may help you to troubleshoot the problem.

leocwlau
Community Advisor and Adobe Champion
leocwlauCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
August 3, 2023

The problem is on the left-hand side of the assignment statement, content.xdm._experience.analytics.customDimensions.eVars.eVar16. You can try to print the "content" out in the console as @yuhuisg suggested and you will find there is no "analytics" sub-object under "_experience". The "content" you have in the callback contains only the XDM passing into the sendEvent call instead of a full scheme structure.

It is a practice, when doing value assignments, to check path existence and construct any necessary sub-object leading to the attribute you are assigning.

yuhuisg
Community Advisor
Community Advisor
August 3, 2023

@leocwlau that's interesting. I would've expected the entire XDM object to be available in content.xdm, including any custom field groups and the _experience.* ones.

leocwlau
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
August 4, 2023

Thank you both! Some helpful context. 

@leocwlau  Do you have an example of how you would 'create' the sub-object?

 

What doesn't make sense to me, is the the sendEvent call I already have other values within the  content.xdm._experience.analytics object, so in theory, it is already defined during the call. It would make more sense to me if it is breaking at the eVar16 (which was not previously set)

 

At the end of the day, the script does actually populate the Analytics Reports, but I would rather eliminate the error (or at the least understand more about its source)


@isaakskinner so as suggested, you can try to print out the xdm from the before sent event to check what you actually have.

Following is the dumbest version of how to check the existence of each level of object and then finally assign a value. You can further search on google or stackoverflow and there should be a better and more generic option.

 

var obj = { level1: { value1: "123", value2: "xyz", }, }; obj.level1 = obj.level1 || {}; obj.level1.level2 = obj.level1.level2 || {}; obj.level1.level2.level3 = obj.level1.level2.level3 || {}; obj.level1.level2.level3.value1 = "abc";

 

samhollingshead
Level 2
February 19, 2024

@isaakskinner I have answered this on another thread but you are able to achieve this if you merge the content.xdm with your custom xdm object you can then assign values to your custom paths e.g.

 

//Merging the action XDM object with the default XDM object
const globalXdmObject = _satellite.getVar('your custom xdm object');
content.xdm = merge globalXdmObject and content.xdm - you could use something like object.assign()

 

You can then write to the custom.xdm following your custom path e.g.

 

content.xdm.mycustompath.campaigntracking code = 123456;

 

I am wondering on reflection whether this is a bug in the Adobe code where its not recognising extended paths in schemas but the above solution has worked for us.

Level 3
February 19, 2024

@samhollingshead  / @leocwlau 

 

Thanks for getting back to me on my issue here.  I'm afraid, I might be missing something here. 

- We have a 'Global' Data Element which sets our global variables (which campaign tracking code is included).

- We have a 'Variable' Data Element that is referenced in our Rules and sets our 'user action specific' variables. 

- And we have a 'Merge' Data Element that gets brings the two together and is referenced in our 'Send Event' Rule Action tile.

 

Unless I'm missing something, this is what you advised, correct?

 

Unfortunately, we still don't have 'campaign tracking code' being set in the 'Click Data Collection' schema and as such we're not collecting the Campaign Tracking Code when the user clicks on links that don't have an associated Rule, such as downloads and exit links.

 

Any thoughts on why after adding 'campaign tracking code' to our Global Data Element, we're still not able to get 'campaign tracking code' to be added to our 'Click Data Collection' schema? 

 

Am I correct in thinking that the 'Click Data Collection' schema can't be modified?  Meaning it's not referencing our 'Global' Data Element by default, and there is no 'Variable' Data Element nor 'Merge' Data Element being referenced because there is no 'rule' to evoke those elements.    As such, where does the 'Click Data Collection' schema come from and how do I modify it so it includes my custom variable, campaign tracking code?  

 

 

samhollingshead
Level 2
February 19, 2024

Apologies, the code example I added above would be included in the "On before link click send callback" found within the Web SDK extension. You can use your merged data element and combine it with content.xdm this should then populate with your custom variables on the link click.

 

/Merging the action XDM object with the default XDM object
const globalXdmObject = _satellite.getVar('your merged XDM object');
content.xdm = combine globalXdmObject and content.xdm - you could use something like object.assign()

 

As you are already populating campaign tracking code this should now happen automatically but doing the above also gives you the ability to update values at this point in the process as @isaakskinner is looking to do e.g.

 

content.xdm._experience.analytics.customDimensions.eVars.eVar16 = digitalData.event.linkClick.href;