This code is a JavaScript snippet for a lightweight analytics...

May 18, 2025 at 11:59 AM

!function(){var e=document.currentScript.getAttribute("data-api")||"https://lb1.beamanalytics.io/api/log";window.beam=function(t){if(!/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(location.hostname)&&"file:"!==location.protocol&&!window.__nightmare&&!window.navigator.webdriver&&!window.Cypress){var a={},n=navigator.languages&&navigator.languages.length?navigator.languages[0]:navigator.userLanguage||navigator.language||navigator.browserLanguage||"en",r=Object.fromEntries(new URLSearchParams(window.location.search).entries()),i="";0>document.referrer.indexOf(location.origin)&&(i=document.referrer);var s=window.beam_token||document.currentScript.getAttribute("data-token");if(window.beam_token=window.beam_token||s,s){a.width=window.innerWidth,a.hostname=location.hostname,a.pathname=t||location.pathname,a.referrer=i,a.user_agent=window.navigator.userAgent,a.locale=n,a.params=r,a.token=s,a.title=document.title,a.hash=location.hash;var o=new XMLHttpRequest;o.open("POST",e,!0),o.setRequestHeader("Content-Type","application/json"),o.send(JSON.stringify(a))}}};var t=function(e){beam()},a=window.history;if(a.pushState){var n=a.pushState;a.pushState=function(){n.apply(this,arguments),beam()},window.addEventListener("popstate",t)}beam()}();

This code is a JavaScript snippet for a lightweight analytics tracking system. Here's a breakdown of what it does:

  1. Setup the Tracking Endpoint:

    • The data-api attribute is used to specify the URL where the analytics data will be sent. If not provided, it defaults to "https://lb1.beamanalytics.io/api/log".
  2. Defines window.beam Function:

    • The beam function is responsible for collecting and sending analytics data. It checks several conditions to ensure tracking is appropriate:
      • Doesn't track if the hostname is localhost, 127.0.0.1, or file: (to avoid local development tracking).
      • Avoids tracking in testing environments like NightmareJS, Cypress, or if run with WebDriver.
    • If tracking is allowed:
      • It collects data such as:
        • width: The browser's viewport width.
        • hostname: The website's hostname.
        • pathname: The current path (or a custom path passed as an argument).
        • referrer: The referrer URL, if applicable and external.
        • user_agent: The browser's user agent.
        • locale: The browser's language or locale settings.
        • params: Any query parameters from the URL.
        • token: A token from the data-token attribute or global window.beam_token.
        • title: The current page's title.
        • hash: Any URL hash values (e.g., #section1).
      • Sends this data as a JSON payload to the tracking API endpoint using an HTTP POST request.
  3. Track on Navigation:

    • It hooks into the window.history.pushState and popstate events to ensure tracking happens when users navigate between pages without fully reloading (for single-page applications).
    • When pushState is called (e.g., navigating to a new route), it triggers the beam function to log the navigation data.
  4. Initial Execution:

    • At the end of the script, the beam function is invoked to track the initial page load.

Summary:

This code tracks user interactions and sends analytics data to a remote server. It gathers details about the user's browser, page navigation, URL parameters, and referrer, then logs this data via POST requests. It integrates well into modern single-page applications by watching for navigation changes.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node