mysterious-addition-11435
02/09/2023, 4:49 PManonymousId every page load. I’ve loaded this into a bundle.js with webpack ( in an effort to cut down on requests on the frontend) - that js bundle is reloaded each time a new page. In the docs - it says clearly - you should run the following code snippet _*once*_ and use the exported object throughout your project . Is there a best way to include this into a bundle.js, load on each page view but check for existing cookies before it replaces them?mysterious-addition-11435
02/09/2023, 4:55 PMimport * as rudderanalytics from "rudder-sdk-js";
export default (function() {
function registerEvents() {
const anchorLinks = document.getElementsByTagName('a');
for (let i = 0; i < anchorLinks.length; i++) {
anchorLinks[i].addEventListener('click', function(event) {
const element = event.currentTarget;
const params = {
eventElement: 'a',
target: element.href,
title: element.innerText,
id_html: element.id,
className: element.className.split(' '),
ariaLabel: element.getAttribute('aria-label'),
};
console.warn(params);
rudderanalytics.track('Clicked A Link', params);
},{once : true})
}
const buttonLinks = document.getElementsByTagName("button");
for (let i = 0; i < buttonLinks.length; i++) {
buttonLinks[i].addEventListener('click', function() {
const params = {
eventElement: 'button',
title: element.innerText,
id_html: element.id,
className: element.className.split(' '),
ariaLabel: element.getAttribute('aria-label'),
};
console.warn(params);
rudderanalytics.track('Pressed A Button',params);
},{once : true})
}
}
function checkIfHtmxIsLoaded(){
setTimeout(function() {
if (window.htmx !== 'undefined'){
registerEvents();
window.htmx.on("htmx:afterSwap", () => {
registerEvents();
});
} else {
checkIfHtmxIsLoaded();
}
}, 500)
}
const CookieDomain = window.location.hostname;
rudderanalytics.load("<key>","<dataPlane>",{
setCookieDomain: CookieDomain
});
rudderanalytics.ready(() => {
// Register Custom Events
checkIfHtmxIsLoaded();
});
rudderanalytics.page();
return { rudderanalytics };
})()mysterious-addition-11435
02/10/2023, 2:40 PMhundreds-lifeguard-65770
02/10/2023, 4:56 PMhundreds-lifeguard-65770
02/10/2023, 4:56 PMmysterious-addition-11435
02/10/2023, 4:59 PMhundreds-lifeguard-65770
02/10/2023, 5:00 PMhundreds-lifeguard-65770
02/10/2023, 5:00 PMmysterious-addition-11435
02/10/2023, 5:01 PMmysterious-addition-11435
02/10/2023, 5:01 PMhundreds-lifeguard-65770
02/10/2023, 5:02 PMhundreds-lifeguard-65770
02/10/2023, 5:02 PMmysterious-addition-11435
02/10/2023, 5:03 PMhundreds-lifeguard-65770
02/10/2023, 5:05 PMmysterious-addition-11435
02/10/2023, 5:07 PMhundreds-lifeguard-65770
02/10/2023, 5:08 PMmysterious-addition-11435
02/10/2023, 5:08 PMhundreds-lifeguard-65770
02/10/2023, 5:09 PMmysterious-addition-11435
02/10/2023, 5:09 PMhundreds-lifeguard-65770
02/10/2023, 5:11 PMhundreds-lifeguard-65770
02/10/2023, 5:11 PMmysterious-addition-11435
02/10/2023, 5:13 PMmysterious-addition-11435
02/10/2023, 5:15 PMmysterious-addition-11435
02/10/2023, 5:15 PMhundreds-lifeguard-65770
02/10/2023, 5:16 PMhundreds-lifeguard-65770
02/10/2023, 5:17 PMmysterious-addition-11435
02/10/2023, 5:19 PMmysterious-addition-11435
02/10/2023, 5:19 PMhundreds-lifeguard-65770
02/10/2023, 5:20 PMmysterious-addition-11435
02/10/2023, 5:21 PM