Hey All - my project is using JavaScript SDK via N...
# support
m
Hey All - my project is using JavaScript SDK via NPM. And I’m having a new
anonymousId
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?
Copy code
import * 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 };
})()
Does anyone have a working example for how they use the npm module to load only once?
h
Are you using a proxied dataplane/sourceConfig - perhaps the cookie is not sticking because its loaded as 3rd party
we proxy all rudder traffic so keep it 1st party and dont have issues with anonymous_id's changing
m
For the proxied dataplane question - would I find that inside of the rudderstack control panel?
h
You need to do some setup on your side, dns etc to make that work
m
Then no. Our data team brought up this project a few days ago. I’m doing some exploratory poking around.
Thank for the article - I’ll look into it more there.
h
we host the js ourselves too making everything 1st party just to make sure
m
This does make sense for hosting the JS - but the docs suggest using npm as a method to use the SDK. My goal was to limit resources requested by including everything into bundle.js
h
yeah I dont use the node package
m
I’m leaning away from it. It may work if the individual JS file - since it’d still have the variables on the global scope. My theory is running it through a preprocesor and modular js the root cause.
h
not sure - it still should be creating a cookie that it should read on the next page
m
And that would make sense. But even a page refresh gets a new cookie.
h
Are you using safari
m
Primarily chrome
h
check the cookie in chrome dev tools, what happens if you remove the set cookie domain btw - also try secureCookie: true
are you on https?
m
Yup
I’ll explore more of the cookieDomain. The overarching request from the data team is to capture that annoymous_id and include it into a form submission.
I’m working on decrypting the ID so that is formats nicely into big query tables.
h
you can create and set your own
m
Cheers 🍻
I’ll look this over.
h
regardless you can still come a cropper against safari/adblock by loading rudder from their dataplane url and sending track events to their domain so best to custom domain it
m
Thanks for the pro tip.
👍 1