@Adam Barth and anyone else that is interested. I was able to figure this out.
In the index.ts file I added a function to update the payload of my page events, basically I am trying to handle outdated UTM parameters and inconsistency.
function cleanRequest(body:any):any {
console.log("Clean Page View");
if (body.hasOwnProperty('context') && body.context.hasOwnProperty('page')) {
let qs = (body.context.page.hasOwnProperty('search')) ? body.context.page.search : "";
let url = (body.context.page.hasOwnProperty('url')) ? body.context.page.url : "";
let queryParams = new URLSearchParams(qs);
let newURL = new URL(url);
let oSource:string = "";
if (! body.context.hasOwnProperty('campaign') ) body.context.campaign = {};
if ( body.context.campaign.hasOwnProperty('source') ) {
oSource = body.context.campaign.source.toLowerCase();
} else if (queryParams.has('utm_source')) {
oSource = (queryParams.get('utm_source') + "").toLowerCase();
}
// const map = sourcemap as CampaignSources;
console.log(JSON.stringify(sourcemap));
let newSrc = sourcemap.find(o => (o.origsrc+"").toLowerCase() === oSource);
if (typeof newSrc !== 'undefined' ) {
body.context.campaign.source = newSrc.source;
body.context.campaign.medium = newSrc.medium;
queryParams.set('utm_source', newSrc.source);
queryParams.set('utm_medium', newSrc.source);
body.context.page.search = "?"+queryParams.toString();
body.context.page.url = newURL.protocol + '//' + newURL.hostname
+ newURL.pathname + "?"+queryParams.toString();
body.properties.search = "?"+queryParams.toString();
body.properties.url =newURL.protocol + '//' + newURL.hostname
+ newURL.pathname + "?"+queryParams.toString();
}
}
return body;
};
In the default function I look for page calls or just pass the original call to Segment.
const url = new URL(request.url);
const parts = url.pathname.split("/");
const method = parts.pop();
let newRequest;
if (method == 'p') {
let body:any = await request.json();
body = cleanRequest(body);
newRequest = new Request(request, { body: JSON.stringify(body) } );
} else {
// newRequest = request;
newRequest = new Request(request);
}
console.log("Write Key: " + env.WRITEKEY);
const segment = new Segment(
{
writeKey: wKey, // writeKey: env.WRITEKEY,
routePrefix: "u", // path prefix for serving Segment assets
personasSpaceId: "...", // optional
personasToken: "...", // optional
},
{
ajsInjection: false,
edgeVariations: false,
proxyOrigin: false,
edgeContext: true,
serverSideCookies: true
}
);
const resp = await segment.handleEvent(newRequest);
return resp;
So effectively adding AJS Middleware functionality at the Edge. The only issue I have with this solution is related to Heap Analytics because it uses its own JS and not PAGE calls which means it doesn’t pick up these changes, but that is more of a Heap issue.