Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@daun/analytics

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@daun/analytics

Best practice analytics.js implementation

  • 0.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Analytics

Easy way to get up and running with Google Analytics.

Personalized version of the original Analytics.js Boilerplate, with a few additions.

  • Tracks uncaught errors
  • Tracks custom user, session, and hit-level dimensions
  • Sends an initial pageview
  • Sends a pageload performance event
  • Autoload the analytics.js script
  • Supports sending custom events and page views

See this article for an in-depth explanation of the features used.

Installation

npm install @daun/analytics

Usage

This package automatically loads the Google Analytics library.

Recommended: load the tracking code lazily so it's non-blocking.

import('@daun/analytics').then(analytics => {
  analytics.init({ /* options */ })
})

Synchronous import is possible, but only recommended for separate entrypoints.

import { init } from '@daun/analytics'

init({ /* options */ })

API

init

Setup the tracker and set initial values.

init({
  trackingId: 'UA-X-XXXXXX',
  timeZone: 'Europe/London'
})

Options

  • trackingId → Google Analytics tracking ID, required
  • timeZone → Time zone (default: Europe/London)
  • anonymizeIp → Anonymize last IP octet? (default: true)
  • removeTrailingSlash → Remove trailing slash (default: true)

trackEvent

Track a custom event.

trackEvent({
  eventCategory: 'Video',
  eventAction: 'play',
  eventLabel: 'Video title'
})

trackPageview

Track a page view. This should only be required in rare cases where autotracking URL changes is not sufficient.

trackPageview('/some/page')

Autotrack

This setup includes a few useful autotrack plugins:

  • clean-url-tracker
  • max-scroll-tracker
  • outbound-link-tracker
  • page-visibility-tracker
  • url-change-tracker

Custom dimensions & metrics

The boilerplate scripts use several custom dimensions and metrics that you'll need to set up within Google Analytics.

Custom dimensions

These can be set up on Google Analytics by going to the Admin section, clicking on Custom Definitions in the PROPERTY column will reveal a link to Custom Dimensions.

Make sure the dimension index number in the admin panel matches up with the number appended to dimension for the corresponding key in the dimensions object in the script you use; i.e. if when you set up the Hit Source dimension it ends up with an index of 12, then the dimensions object should include HIT_SOURCE: 'dimension12'.

NameScript referenceScope
Tracking VersionTRACKING_VERSIONHit
Client IDCLIENT_IDUser
Window IDWINDOW_IDHit
Hit IDHIT_IDHit
Hit TimeHIT_TIMEHit
Hit TypeHIT_TYPEHit
Hit SourceHIT_SOURCEHit
Visibility StateVISIBILITY_STATEHit
Url Query ParamsURL_QUERY_PARAMSHit

Custom metrics

These can be set up on Google Analytics by going to the Admin section, clicking on Custom Definitions in the PROPERTY column will reveal a link to Custom Metrics.

Make sure the metric index number in the admin panel matches up with the number appended to metric in the metrics object in the script you use; i.e. if when you set up the Max Scroll Percentage metric it ends up with an index of 9, then the metrics object should include MAX_SCROLL_PERCENTAGE: 'metric9'.

NameScript referenceScopeFormatting Type
Response End TimeRESPONSE_END_TIMEHitInteger
DOM Load TimeDOM_LOAD_TIMEHitInteger
Window Load TimeWINDOW_LOAD_TIMEHitInteger
Page VisiblePAGE_VISIBLEHitInteger
Max Scroll PercentageMAX_SCROLL_PERCENTAGEHitInteger
Page LoadsPAGE_LOADSHitInteger

Running the boilerplate locally

analytics.js boilerplate uses webpack to compile the source and webpack-dev-server to run it locally.

To install the dependencies and load the boilerplate in a browser, run the following commands:

npm install
npm start

Then visit localhost:8080 in your browser and open the developer console to see the analytics.js debug output.

Running different boilerplate versions

The boilerplate index.js JavaScript file imports the base boilerplate by default. To run a different version, replace the URL imported via import('./analytics/base.js') with the version you want to load.

FAQs

Package last updated on 03 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc