Integration with SvelteKit

SvelteKit integration is not much different than Pure JavaScript integration. You only need to add Metricalp script to your index.html file. Then Metricalp will automatically be integrated to your SvelteKit app and will be starting to tracking screen view events. While we do not have an official SvelteKit library yet (planning it for future), you do not even need that. With this simple steps you will be integrated Metricalp's most recent and stable version always and it will be type safe. In this documentation we will also learn how to fire custom events. With that method, you can also disable auto route catch and fire screen_view events manually. To details please check Custom Events & Props

Add Metricalp script:

We assume that you bootstrapped a new SvelteKit project with official cli like `npm create svelte@latest my-app`. Then inside of your app.html file (src/app.html) between "<head></head>" tags just before the "%sveltekit.head%" add Metricalp script as described in Pure JavaScript Embed

html
<script src="https://cdn.metricalp.com/event/metricalp.js" data-allow-localhost="true" data-tid="YOUR_TID" defer></script>

- Do not forget to change YOUR_TID with your tid number a.k.a Tracker ID as described in Embed Share Tracker

We added allowLocalhost because defaultly Metricalp does not work in localhost to protect your quota limits. We allowed localhost for testing purposes. You would want to remove it after testing it once to save your quota. For detailed info check allow localhost & exclude me docs.

That's all, you integrated your SvelteKit app with Metricalp, hooray 🚀

Defaultly, Metricalp automatically starts to collecting screen_view events and route changes (it can be disabled with data-disable-auto-route-catch option please check API reference for details and all other customizations).

Fire custom events with type safety:

Let's create a new file metricalp-tracker.ts in root of app (src/metricalp-tracker.ts)

typescript

        type MetricalpEvent = {
          type: string;
          [key: string]: string | number | boolean | undefined;
        };
        
        declare global {
          interface Window {
            metricalp?: { event: (ev: MetricalpEvent) => void };
          }
        }
        
        export enum MetricalpEventTypes {
          ScreenView = 'screen_view',
          ClickSave = 'click_save',
        }
        
        export function metricalpEvent(event: MetricalpEvent) {
          if (typeof window === 'undefined' || !window['metricalp']?.event) {
            return;
          }
          window.metricalp?.event(event);
        }
        

Now we can use metricalpEvent function to fire custom events. Let's import and use it in our app. For example (src/routes/+page.svelte) and fire a custom event when user clicks save button.

typescript

      <script lang="ts">
        import { MetricalpEventTypes, metricalpEvent } from '../metricalp-tracker';
      </script>
      
      <button
        on:click={() => metricalpEvent({ type: MetricalpEventTypes.ClickSave })}
      >
        Save</button
      >
      
        

That is all, now you can fire custom events with type safety because we used event types as Enums. You can also disable auto route catch and fire a screen_view event manually with MetricalpEventTypes.ScreenView as we defined above. Also you can attach extra data to event as custom props, for details please check Custom Events & Props

Also please check full API Reference to customize script as you wish: API reference