> ## Documentation Index
> Fetch the complete documentation index at: https://docs.paraminternationalltd.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK Initialization

> Install and initialize the Twinalyze JavaScript Analytics SDK.

export const DownloadWorker = () => {
  const [downloading, setDownloading] = useState(false);
  const workerUrl = "https://raw.githubusercontent.com/jenilmoradiyatwinnet/docs/main/images/twinalyze-fcm-sw.js";
  const handleDownload = async () => {
    try {
      setDownloading(true);
      const response = await fetch(workerUrl, {
        cache: "no-store"
      });
      if (!response.ok) {
        throw new Error("Worker file could not be downloaded.");
      }
      const blob = await response.blob();
      const fileUrl = URL.createObjectURL(blob);
      const link = document.createElement("a");
      link.href = fileUrl;
      link.download = "twinalyze-fcm-sw.js";
      document.body.appendChild(link);
      link.click();
      link.remove();
      URL.revokeObjectURL(fileUrl);
    } catch (error) {
      console.error("[Twinalyze] Download failed:", error);
      window.open(workerUrl, "_blank", "noopener,noreferrer");
    } finally {
      setDownloading(false);
    }
  };
  return <button type="button" onClick={handleDownload} disabled={downloading} className="inline-flex w-fit items-center justify-center rounded-lg border-0 bg-blue-600 px-4 py-2 text-sm font-semibold text-white transition hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-60">
      {downloading ? "Downloading..." : "Download Worker File ↓"}
    </button>;
};

Install the Twinalyze JavaScript Analytics SDK and initialize it once when your website or application starts.

## Add the SDK dependency

Choose your preferred installation method.

<CodeGroup>
  ```bash npm theme={null}
  npm install @twinalyze/web-analytics
  ```

  ```html CDN theme={null}
  <script src="https://cdn.jsdelivr.net/npm/@twinalyze/web-analytics/dist/cdn.global.min.js"></script>
  ```
</CodeGroup>

## Initialize the SDK

Add this Javascript sdk script to your code and your SDK is ready to send events.

<CodeGroup>
  ```javascript npm theme={null}
  import TwinalyzeAnalytics from "@twinalyze/web-analytics";

  TwinalyzeAnalytics.init({
    apiKey: "YOUR_API_KEY",
    secretKey: "YOUR_SECRET_KEY",
  });
  ```

  ```html CDN theme={null}
  <script>
    window.TwinalyzeAnalytics.init({
      apiKey: "YOUR_API_KEY",
      secretKey: "YOUR_SECRET_KEY",
    });
  </script>
  ```
</CodeGroup>

Replace `YOUR_API_KEY` and `YOUR_SECRET_KEY` with the credentials available in your Twinalyze project.

<Info>
  Initialize the SDK only once. After initialization, Twinalyze creates the device and session identifiers and starts collecting supported website events.
</Info>

## Enable Firebase Cloud Messaging

The complete configuration allows you to manage sessions, automatic event tracking, site search, file downloads, and Firebase Cloud Messaging.

<CodeGroup>
  ```javascript npm theme={null}
  import TwinalyzeAnalytics from "@twinalyze/web-analytics";

  TwinalyzeAnalytics.init({
    apiKey: "YOUR_API_KEY",
    secretKey: "YOUR_SECRET_KEY",

    fcm: {
      enabled: true,
      configUrl: "/twinalyze-fcm-sw.js",
      serviceWorkerPath: "/twinalyze-fcm-sw.js",
    },
  });
  ```

  ```html CDN theme={null}
  <script src="https://cdn.jsdelivr.net/npm/@twinalyze/web-analytics/dist/cdn.global.min.js"></script>

  <script>
    window.TwinalyzeAnalytics.init({
      apiKey: "YOUR_API_KEY",
      secretKey: "YOUR_SECRET_KEY",

      fcm: {
        enabled: true,
        configUrl: "/twinalyze-fcm-sw.js",
        serviceWorkerPath: "/twinalyze-fcm-sw.js",
      },
    });
  </script>
  ```
</CodeGroup>

To enable web push notifications, create the following service worker file:

```text theme={null}
public/twinalyze-fcm-sw.js
```

The final file must be publicly available at:

```text theme={null}
https://your-domain.com/twinalyze-fcm-sw.js
```

To enable web push notifications, download the Twinalyze Firebase service worker file.

<div className="not-prose overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-sm dark:border-gray-700 dark:bg-gray-900">
  <div className="border-b border-gray-200 bg-[#ECEEF2] px-5 py-4 dark:border-gray-700 dark:bg-gray-800">
    <p className="m-0 text-base font-semibold text-black dark:text-white">
      Twinalyze FCM Service Worker
    </p>

    <p className="m-0 mt-1 text-sm text-gray-600 dark:text-gray-300">
      Download the worker file and add your Firebase configuration.
    </p>
  </div>

  <div className="flex flex-col gap-4 px-5 py-5 sm:flex-row sm:items-center sm:justify-between">
    <div>
      <code>twinalyze-fcm-sw\.js</code>

      <p className="m-0 mt-2 text-sm text-gray-600 dark:text-gray-300">
        Place this file in your application's public root directory.
      </p>
    </div>

    <DownloadWorker />
  </div>
</div>

## Request notification permission

Notification permission must be requested after a user action, such as clicking an **Enable Notifications** button.

Call the function from a button:

```html theme={null}
<button onclick="enableNotifications()">
  Enable Notifications
</button>
```

```javascript theme={null}
async function enableNotifications() {
  if (!("Notification" in window)) {
    console.log("Notifications are not supported.");
    return;
  }

  const permission =await Notification.requestPermission();

  if (permission === "granted") {
    console.log("Notification permission granted."
    );
  }

  if (permission === "denied") {
    console.log( "Notification permission denied."
    );
  }
}
```

<Info>
  FCM works only when the browser supports notifications, FCM is enabled, the service worker is publicly available, and notification permission is granted.
</Info>

<div className="twinalyze-bottom-nav">
  <a href="/sdks/javascript/analytics/get-started" className="twinalyze-nav-side">
    <span className="twinalyze-nav-arrow">‹</span>
    <span>Previous</span>
  </a>

  <a href="/sdks/javascript/analytics/identify-user" className="twinalyze-nav-center">
    <strong>Identify users</strong>
    <span>Connect website activity with known users.</span>
  </a>

  <a href="/sdks/javascript/analytics/identify-user" className="twinalyze-nav-side">
    <span>Next</span>
    <span className="twinalyze-nav-arrow">›</span>
  </a>
</div>
