jam-cloud/web/app/assets/javascripts/meta_tracking.js

118 lines
4.2 KiB
JavaScript
Raw Normal View History

2026-01-14 14:52:19 +00:00
/**
* meta_tracking.js
* A standalone module to capture and persist Meta attribution signals (fbclid, _fbp) in cookies.
*
* Logic adapted for legacy environment (no React hooks).
* - Checks URL for `fbclid` and sets `_fbc` cookie.
* - Checks for `_fbp` cookie; if missing, generates and sets it.
*/
2026-01-17 04:15:36 +00:00
(function (window, document) {
2026-01-14 14:52:19 +00:00
'use strict';
var MetaTracking = {
2026-01-17 04:15:36 +00:00
init: function () {
2026-01-14 14:52:19 +00:00
var location = window.location;
this.handleFbc(location.search);
this.handleFbp();
2026-01-17 04:15:36 +00:00
this.handleUtm(location.search);
2026-01-14 14:52:19 +00:00
},
// 1. Parsing and storing _fbc (Click ID)
2026-01-17 04:15:36 +00:00
handleFbc: function (searchParams) {
2026-01-14 14:52:19 +00:00
var fbclid = this.getQueryParam('fbclid', searchParams);
2026-01-17 04:15:36 +00:00
2026-01-14 14:52:19 +00:00
if (fbclid) {
var version = 'fb';
var subdomainIndex = 1; // 1 = example.com
var creationTime = new Date().getTime(); // Unix timestamp in ms
2026-01-17 04:15:36 +00:00
2026-01-14 14:52:19 +00:00
// Format: fb.1.timestamp.id
var fbcValue = version + '.' + subdomainIndex + '.' + creationTime + '.' + fbclid;
2026-01-17 04:15:36 +00:00
2026-01-14 14:52:19 +00:00
this.setCookie('_fbc', fbcValue, 90);
}
},
2026-01-17 04:15:36 +00:00
handleUtm: function (searchParams) {
var self = this;
2026-01-24 05:17:35 +00:00
if (!searchParams) return;
// Logically, we want to capture all utm_ parameters.
// We can either iterate a list or dynamic regex.
// Given the requirement to be robust, let's look for "utm_"
var query = searchParams.substring(1); // remove '?'
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (pair.length === 2) {
var key = decodeURIComponent(pair[0]);
var value = decodeURIComponent(pair[1]);
if (key.indexOf('utm_') === 0) {
self.setCookie(key, value, 90);
}
2026-01-17 04:15:36 +00:00
}
}
},
2026-01-14 14:52:19 +00:00
// 2. Handling _fbp (Browser ID)
2026-01-17 04:15:36 +00:00
handleFbp: function () {
2026-01-14 14:52:19 +00:00
if (!this.getCookie('_fbp')) {
var version = 'fb';
var subdomainIndex = 1;
var creationTime = new Date().getTime();
var randomInt = Math.floor(Math.random() * 10000000000); // 10-digit random number
2026-01-17 04:15:36 +00:00
2026-01-14 14:52:19 +00:00
// Format: fb.1.timestamp.randomDigits
var fbpValue = version + '.' + subdomainIndex + '.' + creationTime + '.' + randomInt;
2026-01-17 04:15:36 +00:00
2026-01-14 14:52:19 +00:00
this.setCookie('_fbp', fbpValue, 90);
}
},
// Helper: Get query param by name
2026-01-17 04:15:36 +00:00
getQueryParam: function (name, search) {
2026-01-14 14:52:19 +00:00
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
},
// Helper: Set cookie
2026-01-17 04:15:36 +00:00
setCookie: function (name, value, days) {
2026-01-14 14:52:19 +00:00
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
// Ensure path is root and domain is included if needed (defaults to current host)
document.cookie = name + "=" + (value || "") + expires + "; path=/";
},
// Helper: Get cookie
2026-01-17 04:15:36 +00:00
getCookie: function (name) {
2026-01-14 14:52:19 +00:00
var nameEQ = name + "=";
var ca = document.cookie.split(';');
2026-01-17 04:15:36 +00:00
for (var i = 0; i < ca.length; i++) {
2026-01-14 14:52:19 +00:00
var c = ca[i];
2026-01-17 04:15:36 +00:00
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
2026-01-14 14:52:19 +00:00
}
return null;
}
};
// Initialize on ready
if (document.readyState === 'complete' || document.readyState === 'interactive') {
MetaTracking.init();
} else {
// IE9+ support for DOMContentLoaded
2026-01-17 04:15:36 +00:00
document.addEventListener('DOMContentLoaded', function () {
2026-01-14 14:52:19 +00:00
MetaTracking.init();
});
}
})(window, document);