Skip to content

Alex7Li/measurement-library

 
 

Repository files navigation

Measurement Library

Measurement library will provide an open source alternative for sites/apps to send events to Google Analytics.

This is not an officially supported Google product.

Build Status License Contributions welcome

Table of contents

Overview

Measurement Library is a utility to help developers send data collected from their website to other libraries for analytics. After specifying settings in a script tag, you can process any sent events using a variety of built in event processors (e.g. sending data to Google Analytics).

Data from events or generated by event processors is automatically saved to persist between visits using your choice of a variety of different storage options (e.g. cookies). Read the docs to learn more.

Installation

First, install the package with your favorite package manager:

yarn add measurement-library
// or
npm install measurement-library

Next, choose the event processor and the storage implementation to use and include this code in your <head> tag. It should be placed as high up in the block as possible in order to catch users who leave immediately after loading the page, then immediately leave.

<!-- Measurement Library Snippet -->
<script async src="./node_modules/measurement-library/dist/measure"/>
<script>
  window.dataLayer = window.dataLayer || [];
  function measure(){dataLayer.push(arguments);}
  // One or more config commands. To send data to google analytics while
  // using cookies for storage:
  measure('config', 'googleAnalytics', {
    'measurement_id': YOUR_MEASUREMENT_ID,
    'api_secret': YOUR_API_SECRET,
  }, 'cookies', {});
</script>

As an alternative, you can use the development version when testing your page. The dev version has a bigger file size, but reports possible errors to the console.

<!-- Measurement Library Snippet (Development Version) -->
<script async src="./node_modules/measurement-library/dist/measure-debug"/>
<script>/* Configure script like last example. */ </script>

Usage

After setting up the script tags, you can begin to use the library. All commands are processed by passing arguments to the measure() function.

Event command

When you run the command measure('event', eventName, eventOptions), all registered event processors will read the event and perform actions corresponding to their API.

Set command

To save parameters beyond the current page, you can call the command measure('set', key, value). The registered event processor will determine if the value should be saved, how long the value should be saved for, and save it. To overwrite this behavior, you can specify a third time-to-live parameter: measure('set', key, value, secondsToLive). In particular, if secondsToLive is 0, no data will be saved to long term storage.

The set command can also be used to set parameters related to a implementation of an event processor or storage interface. For example, to modify the default parameters of the cookies storage, run a set command in the script tag before calling config.

// Set the default cookie parameters
measure('set', 'cookies', {prefix: 'my_', expires: 11});
// Use the default cookie parameters: prefix 'my_' and expires 11.
measure('config', 'eventProcessorName', {}, 'cookies', {});
// Override a default cookie parameter: expires is 22 for this storage.
measure('config', 'eventProcessorName', {}, 'cookies', {expires: 22});

Local Setup

Creating The Build

You will need to have either npm or yarn installed.

First, install dependencies with yarn or npm

yarn install
# or
npm install

Next, run the tests using yarn or npm:

yarn test
# or
npm run test

Running Tests Interactively

You can also run the tests "interactively" via Karma directly.

yarn unit
# or
npm run unit

To run the integration tests instead of the unit tests,

yarn integration
# or
npm run integration

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%