Universal Analytics Node.js Tracker

0.5.3 · deprecated · verified Tue Apr 21

This `universal-analytics` package provides a Node.js module for interacting with Google's Universal Analytics (UA) tracking via the Measurement Protocol v1. It enables server-side tracking of user behavior without requiring client-side browser integration. The current stable version is 0.5.3. This library is specifically designed for Universal Analytics, which has been deprecated by Google in favor of Google Analytics 4 (GA4). Consequently, this package is primarily for maintaining existing legacy systems that still rely on Universal Analytics properties, as new UA properties stopped processing data in July 2023, and all UA processing will cease in July 2024. There is no active release cadence as the underlying service is sunsetting, differentiating it from clients for modern GA4 properties.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a Universal Analytics visitor, sending a basic pageview, and sending an event with persistent parameters and an explicit client ID.

const ua = require('universal-analytics');

// Replace with your actual Google Analytics UA ID
const trackingId = process.env.UNIVERSAL_ANALYTICS_ID ?? 'UA-XXXX-XX';

// Initialize a visitor instance. A random UUID will be generated if client ID is omitted.
const visitor = ua(trackingId);

// Track a simple pageview
visitor.pageview('/')
  .send((err) => {
    if (err) {
      console.error('Failed to send pageview:', err);
    } else {
      console.log('Pageview sent successfully to', trackingId);
    }
  });

// Track an event with persistent parameters
const sessionVisitor = ua(trackingId, 'some-user-client-id', { uid: 'user123', strictCidFormat: false });
sessionVisitor.event('Category', 'Action', 'Label', 1, { ipOverride: '127.0.0.1' })
  .send((err) => {
    if (err) {
      console.error('Failed to send event:', err);
    } else {
      console.log('Event sent successfully for user123 to', trackingId);
    }
  });

view raw JSON →