npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@metrical-io/metrical-browser

v1.11.0

Published

Metrical SDK for the browser.

Downloads

751

Readme

metrical-browser

Metrical SDK for the browser.

Installation

To get started with using Metrical Browser SDK, install the package to your project via npm, yarn or script loader.

Installing via package manager

This SDK is available as a package on npm registry named @metrical-io/metrical-browser. You can install the package using npm or yarn CLI.

Using npm CLI

npm install @metrical-io/metrical-browser

Using yarn CLI

# yarn
yarn add @metrical-io/metrical-browser

Import the package into your project and initialize it with your API key.

import { Metrical } from '@metrical-io/metrical-browser';

const client = new Metrical({ writeKey: '<write key>' });

Installing via script tag

This SDK is also available through CDN.

<script type="application/javascript" src="https://cdn.jsdelivr.net/npm/@metrical-io/metrical-browser/dist/index.iife.min.js"></script>
<script type="text/javascript">
const client = new Metrical({ writeKey: '<write key>' });
</script>

Track behavior

Send Event

You can track an event by calling client.track() with the event name and its properties.

client.track({ event_name: 'My Custom Event', properties: { my_property: 'property_value' }});

The following properties are default properties automatically included with every track event:

  • Screen Height
  • Screen Width
  • Referrer
  • Referring Domain
  • Operating System
  • Device Type
  • Browser
  • Browser Version

All events are sent via HTTPS.

Page Views Tracking

Manual

You can track a page view event by calling client.trackPageView(). By default, 'Page View' is used as the event name, and the following properties are recorded:

  • The page title.
  • The page location.
  • The page protocol.
  • The page domain.
  • The page path.
  • The page query parameters.

You can always specify a custom name and add additional properties as shown below:

client.trackPageView({ event_name: 'My Custom Event', properties: { my_property: 'property_value' }}));

Automatic

Page View events can be tracked automatically on every page load by enabling the defaultTrackingConfig.pageViews during client creation (disabled by default), as shown below:

const client = new Metrical({ writeKey: '<write key>', defaultTrackingConfig: { pageViews: { enabled: true }}});

Dynamic page views in single-page applications are tracked on any URL changes by default. You can control this behavior with the defaultTrackingConfig.pageViews.singlePageAppTracking option, as shown below:

// Track any URL changes.
const client = new Metrical({ writeKey: '<write key>', defaultTrackingConfig: { pageViews: { enabled: true, singlePageAppTracking: 'any' }}});

// Track when the path or query string changes, ignoring changes in the hash.
const client = new Metrical({ writeKey: '<write key>', defaultTrackingConfig: { pageViews: { enabled: true, singlePageAppTracking: 'path-with-query' }}});
    
// Only track when the path changes, disregarding changes in the query string or hash.
const client = new Metrical({ writeKey: '<write key>', defaultTrackingConfig: { pageViews: { enabled: true, singlePageAppTracking: 'path' }}});

// Disable dynamic page views tracking in single-page applications.
const client = new Metrical({ writeKey: '<write key>', defaultTrackingConfig: { pageViews: { enabled: true, singlePageAppTracking: 'disabled' }}});

Marketing Attribution Tracking

The library will automatically populate Page View events with any UTM parameters (utm_source, utm_campaign, utm_medium, utm_term, utm_content) or advertising click IDs (dclid, fbclid, gbraid, gclid, ko_click_id, li_fat_id, msclkid, rtd_cid, ttclid, twclid, wbraid) that are present on the page.

This default behavior can be turned off by disabling the defaultTrackingConfig.marketingAttribution option during client creation, as shown below:

const client = new Metrical({ writeKey: '<write key>', defaultTrackingConfig: { pageViews: { enabled: true }, marketingAttribution: false }});

Identify users & companies

You can manage user identity through the client.identify() and client.reset() methods. Utilizing these methods correctly ensures that events are appropriately linked to the user, regardless of their transitions across devices and browsers.

Identify

You can identify a user with a unique ID to monitor their activity across devices and associate them with their events. Once you have the current user's identity, you should call identify() as shown below, typically after they log in or sign up:

client.identify({ user_id: '<user id>' });

Reset

When your users logout you can trigger a reset method which will help reset the user identity. We’ll disassociate all future tracked events from the currently identified user.

client.reset();

Persistent Storage

By default, cookies with a localStorage fallback are used to store state in the browser. You can control this behavior with the storageType option, as shown below:

// Use cookies explicitly.
const client = new Metrical({ writeKey: '<write key>', storageType: 'cookies'});

// Use localStorage explicitly.
const client = new Metrical({ writeKey: '<write key>', storageType: 'localStorage'});

Protecting user data with Metrical

Metrical prioritizes user privacy while providing flexibility in data collection. By default, Metrical is configured to transmit tracking data, but you have options to control this behavior.

Disabling tracking

To prioritize user privacy, you can proactively disable tracking during initialization of the Metrical client. Set the disableTrackingByDefault property to true:

const client = new Metrical({ writeKey: '<write key>', disableTrackingByDefault: true });

Dynamically toggling tracking

Metrical client allows you to dynamically manage tracking based on user preferences or specific scenarios. Use the following methods:

  • client.enableTracking() activates the transmission of tracking data (this is the default state).
  • client.disableTracking() deactivates the transmission of tracking data.

Controlling IP Address and Geolocation Tracking

For more precise control over user privacy, Metrical offers the option to specifically toggle the tracking of IP address and geolocation information. Use the trackIpAndGeolocation property during initialization:

const client = new Metrical({ 
    writeKey: '<write key>', 
    trackIpAndGeolocation: false  // Disable IP and geolocation tracking
});