Skip to content

Commit

Permalink
feat: add utm data mapper for fbclid edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-deriv committed Apr 5, 2024
1 parent 05b11e7 commit b3589f0
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions public/scripts/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const shouldOverwrite = (new_utm_data, current_utm_data) => {
"utm_adgroup_id",
"utm_campaign_id",
"utm_msclk_id",
// For cases where we need to map the query param to some different name e.g [name_from_query_param, mapped_name]
["fbclid", "utm_fbcl_id"],
];

let utm_data = {};
Expand All @@ -110,8 +112,17 @@ const shouldOverwrite = (new_utm_data, current_utm_data) => {

// If the user has any new UTM params, store them
utm_fields.forEach((field) => {
if (searchParams.has(field)) {
utm_data[field] = searchParams.get(field).substring(0, 100); // Limit to 100 supported characters
if (Array.isArray(field)) {
const [field_key, mapped_field_value] = field;
if (searchParams.has(field_key)) {
utm_data[mapped_field_value] = searchParams
.get(field_key)
.substring(0, 200); // Limit to 200 supported characters
}
} else {
if (searchParams.has(field)) {
utm_data[field] = searchParams.get(field).substring(0, 100); // Limit to 100 supported characters
}
}
});

Expand Down Expand Up @@ -205,24 +216,4 @@ const shouldOverwrite = (new_utm_data, current_utm_data) => {
)};domain=${getDomain()}; path=/; SameSite=None; Secure;`;
}
/* end handling gclid */

/* start handling fbclid */
if (searchParams.has("fbclid")) {
const fbclid_data = {};
const fbclid_value = searchParams.get("fbclid");

eraseCookie("fbclid");

if (fbclid_value) {
fbclid_data["fbclid"] = fbclid_value;
}

const fbclid_data_stringified = encodeURI(JSON.stringify(fbclid_data))
.replace(",", "%2C")
.replace("%7B", "{")
.replace("%7D", "}");

document.cookie = `fbclid=${fbclid_data_stringified};domain=${getDomain()}; path=/; SameSite=None; Secure;`;
}
/* end handling fbclid */
})();

0 comments on commit b3589f0

Please sign in to comment.