Expand my Community achievements bar.

SOLVED

How to use Custom Code in AEP Web SDK to send data to Individual profile dataset

Avatar

Level 2

Hi Team,

It has been mentioned in a few posts that the Web SDK can't be used to ingest data from the data layer to individual profiles directly, but custom code can be used. Can anyone please help with the syntax or a sample JS or JSON code to achieve this?

Old, related community posts

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-platform/data-flow-from-aep-web-sd...

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-platform/sending-profile-data-via-...

It would be really helpful if you could provide some pointers on this.

Regards,
Deepan

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hey @e-deepanraj 

 

You can use custom code but one thing you would need to understand is that in that case you would just be making use of the custom code functionality within Adobe data collection tags (launch) and not explicitly using web sdk.

 

You will be making use of a HTTP API Streaming endpoint which would need to be setup under sources within AEP.

 

Then, in your custom code, you would be using that streaming endpoint and sending a POST request to it with a payload.

 

Please note, I would suggest you ask your backend team to handle this rather than you doing it through launch as this can be got hold of and played with easily from Adobe Launch.

 

Sharing a sample JS that should work for you when you fit relevant values for your setup.

 

function lambdaHandler(event, context) {
       const url = "<Your Streaming Endpoint URL>";

    // JSON payload
    const payloadData = {
        header: {
            schemaRef: {
                id: "<Schema ID>",
                contentType: "application/vnd.adobe.xed-full+json;version=1.0"
            },
            imsOrgId: "<YOUR ORG ID>",
            datasetId: "<YOUR DATASET ID>",
            source: {
                name: "Streaming dataflow - 10/15/2023, 2:30 PM"
            }
        },
        body: {
            xdmMeta: {
                schemaRef: {
                    id: "<Your Schema ID>",
                    contentType: "application/vnd.adobe.xed-full+json;version=1.0"
                }
            },
            xdmEntity: {
                _id: "/uri-reference",
                _repo: {
                    createDate: "2023-12-11T12:00:00-06:00",
                    modifyDate: "2023-12-11T12:00:00-06:00"
                },
                _spnam: {
                    identification: {
                        core: {
                            
                            email: '<your email>'
                        }
                    }
                },
                person: {
                    name: {
                        firstName: 'test league'
                    }
                }
            }
        }
    };

    // Convert the payload to JSON
    const payloadJson = JSON.stringify(payloadData);

    // Headers
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('sandbox-name', '<Sandbox Name>');
    headers.append('Authorization', 'Bearer YOUR_AUTHORIZATION_TOKEN'); // Replace with your actual Authorization token

    // Request options
    const options = {
        method: 'POST',
        headers: headers,
        body: payloadJson
    };

    // Make the POST request
    fetch(url, options)
        .then(response => {
            if (response.status === 200) {
                return response.text();
            } else {
                throw new Error("Failed to call the API");
            }
        })
        .then(data => {
            console.log(data);
        })
        .catch(error => {
            console.error(error);
        });
}

// Example usage:
const event = {
};

lambdaHandler(event, null);

 I removed a few bits which were not relevant and you might need to tweak it a bit but this should give you a start. 

View solution in original post

13 Replies

Avatar

Correct answer by
Community Advisor

Hey @e-deepanraj 

 

You can use custom code but one thing you would need to understand is that in that case you would just be making use of the custom code functionality within Adobe data collection tags (launch) and not explicitly using web sdk.

 

You will be making use of a HTTP API Streaming endpoint which would need to be setup under sources within AEP.

 

Then, in your custom code, you would be using that streaming endpoint and sending a POST request to it with a payload.

 

Please note, I would suggest you ask your backend team to handle this rather than you doing it through launch as this can be got hold of and played with easily from Adobe Launch.

 

Sharing a sample JS that should work for you when you fit relevant values for your setup.

 

function lambdaHandler(event, context) {
       const url = "<Your Streaming Endpoint URL>";

    // JSON payload
    const payloadData = {
        header: {
            schemaRef: {
                id: "<Schema ID>",
                contentType: "application/vnd.adobe.xed-full+json;version=1.0"
            },
            imsOrgId: "<YOUR ORG ID>",
            datasetId: "<YOUR DATASET ID>",
            source: {
                name: "Streaming dataflow - 10/15/2023, 2:30 PM"
            }
        },
        body: {
            xdmMeta: {
                schemaRef: {
                    id: "<Your Schema ID>",
                    contentType: "application/vnd.adobe.xed-full+json;version=1.0"
                }
            },
            xdmEntity: {
                _id: "/uri-reference",
                _repo: {
                    createDate: "2023-12-11T12:00:00-06:00",
                    modifyDate: "2023-12-11T12:00:00-06:00"
                },
                _spnam: {
                    identification: {
                        core: {
                            
                            email: '<your email>'
                        }
                    }
                },
                person: {
                    name: {
                        firstName: 'test league'
                    }
                }
            }
        }
    };

    // Convert the payload to JSON
    const payloadJson = JSON.stringify(payloadData);

    // Headers
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('sandbox-name', '<Sandbox Name>');
    headers.append('Authorization', 'Bearer YOUR_AUTHORIZATION_TOKEN'); // Replace with your actual Authorization token

    // Request options
    const options = {
        method: 'POST',
        headers: headers,
        body: payloadJson
    };

    // Make the POST request
    fetch(url, options)
        .then(response => {
            if (response.status === 200) {
                return response.text();
            } else {
                throw new Error("Failed to call the API");
            }
        })
        .then(data => {
            console.log(data);
        })
        .catch(error => {
            console.error(error);
        });
}

// Example usage:
const event = {
};

lambdaHandler(event, null);

 I removed a few bits which were not relevant and you might need to tweak it a bit but this should give you a start. 

Avatar

Level 2

Thanks @abhinavbalooni Let me check with this JS,

I was just curious about the Datastream AEP service. It has a profile dataset as an optional selection, so is there any use in having that to send the profile data, or is it a feature that is not fully functional yet?

edeepanraj_0-1702289238147.png

 

Avatar

Community Advisor

@e-deepanraj That's a fair query and I had the same question when I started working on data streams but till now I haven't seen any use for it or even documentation showing how it works. Might well be a solution in works for now.

 

Cheers.

Avatar

Level 2

Thanks @abhinavbalooni I was able to follow your custom code and ingest profile data using the custom code with HTTP API in the action of a rule created. However,  I have a concern with the authorization bearer token being exposed when checked with the browser developer tools. Is there anything that you can recommend to hide it or any way to generate it on demand so that theauthorization token is secured.

Avatar

Community Advisor

Hey @e-deepanraj 

That's the exact reason I mentioned in my initial response that you should not be doing these piece client side or through Adobe Launch as you leave yourself exposed to injection attacks through the vulnerabilities.

I can provide you a script to generate access token but would strongly suggest not going down this path. Your backend team would be a much better place to all the HTTP API streaming endpoints.

 

Let me know if you still want a script to generate token and I can share one.

 

Cheers,

Abhinav

Avatar

Level 2

Sure @abhinavbalooni , thanks. Could you please share the script to generate the token?

Avatar

Moderator

So it's not with the webSDK directly but within datastreams.  https://experienceleague.adobe.com/docs/experience-platform/datastreams/configure.html?lang=en

 

""Profile Dataset Select the Platform dataset that customer attribute data will be sent to. This schema must use the XDM Individual Profile class."" 

 

Disregard my comment above.  This can NOT be used to send profile data.  Working to get the UI & Docs updated to reflect that.  Sorry for any confusion. 

Avatar

Community Advisor

@Rudi-Shumpert Thanks for this Rudi.

 

Do you have any documentation where we can see this mapping of the profile dataset being used through a datastream? Would be really helpful as I haven't been able to make use of this particular mapping till date. Might be an oversight at my end.

 

Cheers,

Abhinav

Avatar

Moderator

Disregard my comment above.  This can NOT be used to send profile data.  Working to get the UI & Docs updated to reflect that.  Sorry for any confusion. 

Avatar

Level 2

Thanks, @Rudi-Shumpert, same doubt as @abhinavbalooni on how to make use of the mapping. If you could share a document, that would be really helpful. So webSDK doesn't have out-of-box functionality to handle the profile data directly ?

Avatar

Moderator

Disregard my comment above.  This can NOT be used to send profile data.  Working to get the UI & Docs updated to reflect that.  Sorry for any confusion. 

Avatar

Level 2

No problem @Rudi-Shumpert , Thanks for confirming that the datastreams cannot be used to send the profile data to a dataset of a schema of type XDM Individual Profile class.

 

If possible, could you please let me know if there are any options via Web SDK or any other Tags Extension that can be used to achieve this, apart from what @abhinavbalooni has suggested?