Examples

A single HTML page that shows what the Browser SDK actually sends, straight from the browser.

One CDN script and a few buttons, enough to watch the instrumentation work. See Installation for the other install methods and Instrumentation for what each button exercises.

Single HTML page

Open index.html and add the following code. If a valid collector URL is not configured (for example, due to a private network), an error will occur during export, but you can still verify Trace and Log data directly in the Network tab of the browser DevTools.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Sophonz Browser SDK Test Example</title>
    <script
      src="https://cdn.sophonz.com/release/npm/@browser-sdk/latest/index.js"
      crossorigin="anonymous">
    </script>
    <script>
      SophonzSDK.init({
        collectorUrl: "https://in.sophonz.ai",
        appName: "your-app-name",
        appKey: "YOUR_APP_KEY",
        appVersion: "1.0.0"
      });
    </script>
  </head>
  <body>
    <h1>Sophonz Browser SDK Test Example</h1>
    <button id="work-btn">Do Work</button>
    <button id="error-btn">Throw Error</button>
    <button id="fetch-btn">Fetch Data</button>
    <button id="xhr-btn">XHR</button>
    <p>Click the buttons to test Sophonz tracing and error reporting.</p>
  </body>
  <script>
    // console instrumentation
    function doWork() {
      console.log("Work is done and span is ended");
    }
    // error instrumentation
    function throwError() {
      throw new Error("Test Error");
    }
    // xhr instrumentation
    function xhrBtn() {
      console.log("xhrBtn");
      const xhr = new XMLHttpRequest();
      xhr.open("GET", "https://jsonplaceholder.typicode.com/todos/1");
      xhr.onload = function () {
        if (xhr.status === 200) {
          console.log(xhr.response);
        }
      };
      xhr.send();
    }
    // fetch instrumentation
    function fetchBtn() {
      console.log("fetchBtn");
      fetch("https://jsonplaceholder.typicode.com/todos/1")
        .then(function (response) {
          return response.json();
        })
        .then(function (json) {
          console.log(json);
        });
    }
    // Attach event handlers
    window.onload = () => {
      document.getElementById("work-btn").addEventListener("click", () => {
        doWork();
      });
      document.getElementById("error-btn").addEventListener("click", () => {
        throwError();
      });
      document.getElementById("xhr-btn").addEventListener("click", () => {
        xhrBtn();
      });
      document.getElementById("fetch-btn").addEventListener("click", () => {
        fetchBtn();
      });
    };
  </script>
</html>