Measurement protocol for human

Usage no npm install needed!

<script type="module">
  import measurementProtocol from '';



npm version Bundle size Types

A js module for Google's Universal Analytics tracking via the Measurement Protocol, in human friendly manner.

  • Works universal (node.js / browser)
  • Complete type annotations
  • Minimal footprint


npm install measurement-protocol
const { measure } = require('measurement-protocol')

// Initialize with Google Analytics Tracking ID / Web Property ID:

Core Functions

To send measurement data to Google Analytics, all you need is:

measure(trackId)  // create a measurement instance
  .set(params)    // setup parameters
  .send()         // send it
  • trackId Google Analytics Tracking ID / Web Property ID ('UA-XXXXX-XX')
  • params Parameters for Measurement Protocol ({ t: 'pageview', dh: '', dp: '/docs' })

For all available parameters, checkout Measurement Protocol Parameter Reference


Built on top of that, there's human-friendly helpers for common usage:

// a human fiendly manner
measure(trackId).pageview({ host: '', path: '/docs' }).send()
// equals to
measure(trackId).set({ t: 'pageview', dh: '', dp: '/docs' }).send()



measure(trackId: string, params?: Record<string, string>) => Measurement

Create a measurement instance.

// Create a measurement (tracker)
const tracker = measure('UA-XXXXX-XX')
// Create a measurement with params
const tracker = measure('UA-XXXXX-XX', { uid: 'XXXX.XXXX' })

All measurement methods are chainable (returns a new Measurement instance), except .send().


measurement.set(params: Record<string, string>)

Set measurement parameter, returns a new measurement instance.

const tracker = measure('UA-XXXXX-XX')

const trackPageview = tracker.set({ t: 'pageview' })
const trackEvent = tracker.set({ t: 'event' })


measurement.send() => Promise<Response | IncomingMessage>

Send measurement data to Google Analytics via Measurement Protocol.


measurement.pageview(url: string | { host: string, path: string })

Page view measurement allows you to measure the number of views you had for a particular page on your website. Pages often correspond to an entire HTML document, but they can also represent dynamically loaded content; this is known as "virtual pageviews".

measure('UA-XXXXX-XX').pageview({ host: '', path: '/about' }).send()


measurement.screenview(screenName: string)

Screens represent content users are viewing within an app. The equivalent concept for a website is pages. Measuring screen views allows you to see which content is being viewed most by your users, and how are they are navigating between different pieces of content.

measure('UA-XXXXX-XX').screenview('High Scores').send()

Other params can be used with screenview measurement:

  • an Application Name ('My App')
  • av Application version (1.2.0)
  • aid Application ID (
  • aiid Application Installer ID (com.platform.vending)
measure('UA-XXXXX-XX').set({ an: 'My App', av: '1.2.0' }).screenview('High Scores').send()


measurement.transaction(id: string, affiliation?: string, revenue = 0, shipping = 0, tax = 0)

measure('UA-XXXXX-XX').transaction('OD564', 'Member', 15.47, 3.50, 11.20).send()


measurement.item(id: string, name: string, price = 0, quantity = 0, code?: string, category?: string)

measure('UA-XXXXX-XX').item('0D564', 'Shoe').send()
measure('UA-XXXXX-XX').item('0D564', 'Shoe', 3.50, 4, 'SKU47', 'Blue').send()

.social() string, action: string, actionTarget: string)

You can use social interaction analytics to measure the number of times users click on social buttons embedded in webpages. For example, you might measure a Facebook "Like" or a Twitter "Tweet".

While event measurement can help you analyze general user-interactions very well, Social Analytics provides a consistent framework for recording social interactions. This in turn provides a consistent set of reports to compare social network interactions across multiple networks.

measure('UA-XXXXX-XX').social('facebook', 'like', '').send()


measurement.event(category: string, action: string, label?: string, value?: number)

Events are user interactions with content that can be measured independently from a web page or a screen load. Downloads, mobile ad clicks, gadgets, Flash elements, AJAX embedded elements, and video plays are all examples of actions you might want to measure as Events.

measure('UA-XXXXX-XX').event('error', '404').send()
measure('UA-XXXXX-XX').event('error', '404', '/not-found').send()


measurement.timing(category: string, name: string, value: number, label?: string)

User timings allow developers to measure periods of time. This is particularly useful for developers to measure the latency, or time spent, making AJAX requests and loading web resources.

measure('UA-XXXXX-XX').timing('deps', 'load', 3200).send()
measure('UA-XXXXX-XX').timing('deps', 'load', 3200, 'css').send()


measurement.exception(description: string, fatal?: boolean = true)

Exception tracking allows you to measure the number and type of crashes or errors that occur on your property.

measure('UA-XXXXX-XX').exception(error.message, false).send()


batchSend(measurements: Measurement[]) => Promise<Response | IncomingMessage>

Send multiple hits (measurement) in a single request. Batch requests to Google Analytics have the following limitations:

  • A maximum of 20 hits can be specified per request.
  • The total size of all hit payloads cannot be greater than 16K bytes.
  • No single hit payload can be greater than 8K bytes.
const { measure, batchSend } = require('measurement-protocol')

  measure('UA-XXXXX-XX').event('load', '/index.js')