New:Microsoft Teams Notifications Are Now Available in Socket.Learn more →
Get Started

measureprotocol

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

measureprotocol

A Node.js library for sending custom event data to Google Analytics 4 via the Measurement Protocol.

latest
npmnpm
Version
1.0.0
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

Measure Protocol

Measure Protocol is a Node.js library designed to simplify integration with Google Analytics 4 (GA4) through the Measurement Protocol. It enables the straightforward sending of custom event data to GA4, ideal for Node.js applications requiring robust data analysis.

Installation

To install the library, use npm or yarn by running one of the following commands in your terminal:

npm install measure-protocol

ou

yarn add measure-protocol

Initial Setup

To start using the Measure Protocol library, first import and configure an instance with your GA4 credentials:

const MeasureProtocol = require("measure-protocol"); // Adjust the path as needed

const analytics = new MeasureProtocol({
  debug: true, // Enable debug mode for detailed logs
  measurementId: "YOUR_MEASUREMENT_ID", // Replace with your GA4 Measurement ID
  apiSecret: "YOUR_API_SECRET", // Replace with your GA4 API Secret
});

Basic Usage

Sending a Page View Event You can send custom events to GA4. Here is an example of how to send a page view event:

Copy code
async function sendPageView(clientId, sessionId) {
  const params = {
    page_title: "Homepage",
    page_location: "https://www.example.com",
    page_path: "/",
    engagement_time_msec: 5000, // Engagement time on the page in milliseconds
  };

  try {
    const { clientId, sessionId } = await analytics.fireEvent("page_view", params);
    console.log(clientId, sessionId);
    console.log("Page view event successfully sent.");
  } catch (error) {
    console.error("Error sending page view event:", error);
  }
}

let clientId = null; // Initially null, generate or retrieve as needed
let sessionId = null; // Initially null, generate or retrieve as needed

// Call the function to send an event
sendPageView(clientId, sessionId);

Keywords

google-analytics

FAQs

Package last updated on 11 Mar 2024

Related posts