let filtripixel_trackingId = 0;
let filtripixel_pixelId = '';
let filtripixel_interactionEnabled = false;
let filtripixel_lastScrollPosition = 0;
let filtripixel_lastReportedScrollPercentage = 0;
let filtripixel_verify_installation = false;
function filtripixel_preventInteraction(event) {
if (!filtripixel_interactionEnabled) {
event.preventDefault();
event.stopPropagation();
}
}
function filtripixel_getTrackingId(params) {
const body = {
events: [
{
type: 'visit',
...params,
},
],
pixelId: filtripixel_pixelId,
};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
};
.then((response) => response.json())
.then((response) => {
filtripixel_trackingId = response.trackingId;
filtripixel_setTrackingIdOnAllLinks(response.trackingId);
})
.catch((err) => {
filtripixel_interactionEnabled = true;
document.removeEventListener('click', filtripixel_preventInteraction, true);
});
}
function filtripixel_sendEvents(params, eventType) {
const body = {
trackingId: filtripixel_trackingId,
pixelId: filtripixel_pixelId,
events: [
{
type: eventType,
...params,
},
],
};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
};
filtripixel_interactionEnabled = true;
document.removeEventListener('click', filtripixel_preventInteraction, true);
});
}
function filtripixel_getParamsFromQueryString() {
const currentUrl = new URL(window.location.href);
const params = new URLSearchParams(currentUrl.search);
const visitParameters = {};
params.forEach((value, key) => {
visitParameters[key] = value;
if (key === 'filtripixel-installation-verify') {
if (value === filtripixel_pixelId) {
alert('Filtripixel instalado com sucesso');
}
}
});
filtripixel_getTrackingId(visitParameters);
}
function filtripixel_setTrackingIdOnAllLinks(trackingId) {
const links = document.querySelectorAll('a');
links.forEach((link) => {
if (link.href) {
try {
const linkUrl = new URL(link.href);
const params = ['tid', 'subid', 'sid1', 'sck', 'src'];
params.forEach((param) => {
linkUrl.searchParams.set(param, trackingId);
});
link.href = linkUrl.href;
filtripixel_interactionEnabled = true;
document.removeEventListener('click', filtripixel_preventInteraction, true);
const readyDiv = document.createElement('div');
readyDiv.id = 'filtripixel_is_ready';
readyDiv.style.display = 'none';
document.body.appendChild(readyDiv);
} catch (error) {
console.error(error);
}
}
});
}
function filtripixel_calculateScrollPercentage() {
const scrolled = window.scrollY;
const viewportHeight = window.innerHeight;
const fullHeight = document.documentElement.scrollHeight;
const scrolledPercentage = Math.min(((scrolled + viewportHeight) / fullHeight) * 100, 100);
return scrolledPercentage;
}
function filtripixel_handleScrollEvent() {
const currentScrollPosition = window.scrollY;
const currentScrollPercentage = filtripixel_calculateScrollPercentage();
const scrollDirection =
currentScrollPosition > filtripixel_lastScrollPosition ? 'scroll-down' : 'scroll-up';
const reportingThreshold = 10;
if (
Math.abs(currentScrollPercentage - filtripixel_lastReportedScrollPercentage) >=
reportingThreshold
) {
filtripixel_sendEvents({ percentage: Math.round(currentScrollPercentage) }, scrollDirection);
filtripixel_lastReportedScrollPercentage = currentScrollPercentage;
}
filtripixel_lastScrollPosition = currentScrollPosition;
}
function filtripixel() {
filtripixel_getParamsFromQueryString();
document.addEventListener('click', function (event) {
let target = event.target;
while (target && target !== document.body && target.nodeName !== 'A') {
target = target.parentNode;
}
if (target && target.nodeName === 'A') {
const params = Array.from(target.attributes).reduce((acc, attr) => {
acc[attr.name] = attr.value;
return acc;
}, {});
filtripixel_sendEvents(params, 'click');
}
});
window.addEventListener('scroll', filtripixel_handleScrollEvent);
}
document.addEventListener('DOMContentLoaded', function () {
document.addEventListener('click', filtripixel_preventInteraction, true);
const scriptTags = document.getElementsByTagName('script');
for (let script of scriptTags) {
if (script.src.includes('filtripixel.minify.js')) {
const url = new URL(script.src);
filtripixel_pixelId = url.searchParams.get('pixelId');
if (filtripixel_pixelId) {
filtripixel();
break;
}
}
}
});