Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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. 

 

uncaughterror.PNG

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

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

7 Replies

Avatar

Community Advisor

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.

Avatar

Correct answer by
Community Advisor

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.

Avatar

Community Advisor

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

Avatar

Community Advisor

By the time of sendEvent, there is no information to WebSDK on the actual XDM schema of the incoming object which is assigned at the Datatream where we can map attributes of the incoming XDM to attributes on the Event Schema assigned to the Datatream.

So the incoming XDM to WebSDK and available in the before-event callback is actually a free-form JSON and has no guarantee on the structure.

Another consideration is the XDM schema can be huge depending on how many field groups are added to the schema, so even if we can configure WebSDK with the XDM schema to be used, it will have performance issues if Adobe wants to populate the XDM structure and apply schema check on the frontend, which leads me to believe Adobe won't do that.

Avatar

Level 2

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)

Avatar

Community Advisor

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

 

Avatar

Level 2

Thank you both for the replies

Ultimately i decided against this approach. I beleive Adobe's intended approach anyway would be to updated the field group values, and then set processing rules to update the Context 

 

I decided to add any of the custom code in the Data Elements of the values themselves, avoiding the need for event Callback.