top of page

Atomic Fusion

Anonymous User Tracking in Bubble.io: How to Implement Custom Cookies

Updated: Sep 26, 2023



This article dives into my experience and insights into enhancing user tracking in Bubble.io through the implementation of persistent cookies. In the evolving landscape of web development, the ability to track user activities is crucial, especially when aiming to deliver a seamless and personalized user experience. While Bubble.io does offer a default user cookie, it has a limited duration of 24 hours. I found myself in a scenario where a more extended tracking period was essential, and thus, embarked on a journey to implement persistent cookies, enabling tracking over a span of 60 days.




The Objective Behind Extended Tracking:

The essence of implementing persistent cookies is to seamlessly connect different activities performed by the same user over varying time frames. This becomes particularly vital when allowing users to interact with functionalities without logging in, ensuring a coherent connection of their activities across different sessions and interactions.


Why I Needed Extended Tracking:

Bubble.io inherently stores a current user cookie, maintaining the user ID for a day. However, the need for tracking user interactions goes beyond this time frame in many instances. Connecting a user’s activities from different sessions provides invaluable insights into user behavior and preferences, allowing for the delivery of more personalized and user-centric experiences.


My Strategy for Implementation:

To implement persistent cookies in Bubble.io, I leveraged the Toolbox plugin to execute custom JavaScript. I developed two distinct JavaScript functions—one to retrieve a cookie (getCookie) and another to set a cookie (setCookie) if the retrieved cookie is non-existent or empty. This approach, although simple, proved to be highly effective in achieving the desired extended user tracking.




Utilizing the Toolbox Plugin:

The Toolbox plugin in Bubble.io was pivotal for running custom JavaScript, enabling the creation and retrieval of custom cookies. I executed two scripts using this plugin: one to get a cookie and another to create a cookie if it didn’t already exist. The JavaScript to Bubble elements served as a conduit, allowing the transfer of information from the JavaScript code to a Bubble element, acting as a temporary storage, enabling the retrieval of the stored data anywhere within the app.





Workflow and Execution:

I initiated an action to run on page load. This action triggers the getCookie function, which searches for a cookie named "viewer". If the cookie is empty, the app sets a new cookie using the setCookie function. If not, the app displays the existing cookie value. This workflow ensures the consistency of the cookie value across different sessions, allowing for accurate tracking of user activities over extended periods.



Achieving Consistency Across Sessions:

Maintaining the consistency of the cookie value across different sessions is crucial for accurate user tracking. The cookie value remains consistent, whether a user interacts with the app in an incognito tab or returns after several days. This consistency enables the app to associate the current user session with a unique identifier stored in the cookie value, allowing for the continuous monitoring of a unique anonymous user's activities.


Application and Practical Use Cases:

The implementation of persistent cookies has diverse applications. In my case, I stored the unique ID of a viewer in the cookie value and conducted a database query with that unique ID value to pick up the actual viewer that the current user's session references. This method allowed me to associate the current user session with a viewer in the database, enabling the continuous tracking of their activity, which is crucial for enhancing user engagement and delivering personalized user experiences.


Code Snippets:


Get a Cookie:

// getCookie


// Function to get a cookie
function getCookie(name) {
  const cookies = document.cookie.split('; ');
  for (let i = 0; i < cookies.length; i++) {
    const cookiePair = cookies[i].split('=');
    if (cookiePair[0] === name) {
      return cookiePair[1];
    }
  }
  return null;
}

// Run the getCookie function, store on a variable
const viewer = getCookie('viewer');

// send the resulting Cookie to the JavascriptToBubble element
if (viewer) {
  // if found, send the Cookie value
  console.log(`Viewer: ${viewer}`);
  bubble_fn_getCookie(viewer);
} else {

  // if not found, send a text "EMPTY"
  console.log('Cookie not found');
  bubble_fn_getCookie("EMPTY");
}

Set a Cookie:

// Function to set a cookie
function setCookie(name, value, days) {
  const date = new Date();
  date.setDate(date.getDate() + days);
  document.cookie = `${name}=${value}; expires=${date.toUTCString()}; path=/`;
}

// defining new Cookie value
var viewer_id = "<INSERT_VALUE_HERE>"

// setting new Cookie called 'VIEWER' with custom value and a lifetime of 60 days
setCookie('viewer', viewer_id, 60);

Privacy and Ethical Considerations:

While the benefits of implementing persistent cookies are numerous, it is imperative to adhere to privacy policies and GDPR regulations. It’s crucial to avoid tracking any personal information without user consent and to ensure compliance with data protection regulations to maintain user trust and avoid legal complications. Balancing innovation with ethical considerations is the key to creating user-centric and compliant web applications.


Conclusion & Final Thoughts:

Implementing persistent cookies in Bubble.io using JavaScript is a powerful method to achieve extended user tracking. The use of the Toolbox plugin and the development of custom JavaScript functions enable the seamless setting and retrieval of cookies. However, it’s equally important to use such features ethically and in compliance with legal norms.


Custom cookies open up avenues for developers to explore innovative features and enhance the functionality of their Bubble apps. By adhering to privacy norms and leveraging the potential of extended user tracking responsibly, developers can truly harness the power of Bubble.io to create impactful and user-friendly web applications.


Happy Bubbling,

Ranjit




354 views0 comments

コメント


bottom of page