agreeable-australia-13022
11/08/2022, 7:37 PMURLSearchParams but it is not defined.hundreds-lifeguard-65770
11/08/2022, 8:02 PMagreeable-australia-13022
11/08/2022, 8:11 PMconst search = event.context?.page?.search;
if (search)
{
const params = new URLSearchParams(search);
const foo = params.get("foo");
log("The value of foo is", foo);
}
and received this output:
{
"error": "ReferenceError: URLSearchParams is not defined"
}agreeable-australia-13022
11/17/2022, 1:32 AMagreeable-australia-13022
12/15/2022, 1:29 AMgentle-petabyte-80785
12/17/2022, 6:55 AMURLSearchParams out of the box in transformer code. So the error shown is correct, there's no such object named URLSearchParams. We can take this as a feature request, for this, please create an issue on GitHub
And meanwhile, you do need to create the functionality of URLSearchParams yourself.
I am sharing a code sample snippet which can help you get started with that
const searchParams = e.context.page ? e.context.page.search : undefined;
if(searchParams) {
const indvSearchParams = searchParams.trim().split('&'); // ?test1=val1&test2=val2
if(indvSearchParams.length > 0) {
const firstSearchString = indvSearchParams[0].slice(1, indvSearchParams[0].length); // remove the preceding '?'
indvSearchParams[0] = firstSearchString;
indvSearchParams.forEach(valSearch => {
var keyValPairs = valSearch.split("="); // seperate key and val
if(keyValPairs[0].length > 0) {
if(!e.context.traits) {
e.context.traits = {};
}
// will be e.context.traits[keyValPairs[0]] = keyValPairs[1], for now keeping key "UTM" and overiding
if (keyValPairs[0] === 'type') {
e.context.traits["UTM"] = keyValPairs[1];
}
if (keyValPairs[0] === 'utm_campaign') {
e.context.traits["UTMCMPG"] = keyValPairs[1];
}
}
})
}
}gentle-petabyte-80785
12/17/2022, 6:57 AMURLSearchParams out of the box so you can be notified when we release this. And also this can help other community members contribute to the discussion.