npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

meh-activity-logger

v2.1.0

Published

Log custom events and pageviews with opinionated defaults to Google Analytics through Express middleware, Node or the browser.

Downloads

110

Readme

MEH Activity Logger

Log custom events and pageviews with opinionated defaults to Google Analytics through Express middleware, Node or the browser.

Install

yarn add meh-activity-logger

Usage

Event logging example in all three environments with the same outcome:

// 1. Node with Express
import { expressMiddleware as mehActivityLogger } from 'meh-activity-logger';
import express from 'express';

express()
  .use(mehActivityLogger('UA-XXXXXX-X')) // Equivalent to: mehActivityLogger({ tid: 'UA-XXXXXX-X' })
  .get('/example', (req, res) => {
    req.event('Example event').catch(console.error); // Equivalent to: event({ action: 'Example event' })
    res.sendStatus(200);
  })
  .listen(3000);
// 2. Node without Express
import mehActivityLogger from 'meh-activity-logger';

const { event } = mehActivityLogger('UA-XXXXXX-X');

event({
  action: 'Example event',
  clientId: '0.0.0.0',
  uip: '0.0.0.0',
  ua:
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
  dr: 'https://www.example.com/',
  dh: 'example.com',
  dp: '/example',
}).catch(console.error);
<!-- 3. Browser with JavaScript -->
<script src="https://unpkg.com/meh-activity-logger"></script>
<script>
  var logger = window.mehActivityLogger.default({
    tid: 'UA-XXXXXX-X',
    appName: 'example-app',
    appVersion: '1.0.0',
  });

  logger
    .event({
      action: 'Example event',
      clientId: '0.0.0.0',
      uip: '0.0.0.0',
    })
    .catch(console.error);
</script>

Will all result in:

// POST https://www.google-analytics.com/collect (form data)

v=1
&tid=UA-XXXXXX-X
&ua=Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_15_4%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F81.0.4044.138+Safari%2F537.36
&dr=https%3A%2F%2Fwww.example.com%2F
&dh=example.com
&dp=%2Fexample
&uip=0.0.0.0
&t=event
&cid=2065339419
&ec=Primary+KPI
&ea=Example+event
&an=example-app
&aid=example-app
&cd1=example-app
&av=1.0.0
&cd2=1.0.0

API

activityLogger([measurementId|properties])

  • Returns: Function (event)

Sets global properties for all GA events.

measurementId

  • Type: String

Measurement ID (format: "UA-XXXXXX-X"). Equivalent to { tid: 'UA-XXXXXX-X' }.

properties (GA Measurement Protocol)

  • Type: Object

Defaults:

| Key | Default value | | -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | v (Protocol Version) | 1 | | tid (Measurement ID) | process.env.MEH_ACTIVITY_LOGGER_MEASUREMENT_ID or 'UA-26548270-15' (defaults to MEH property) | | uip (IP Override) | req.ip (anonymized in GA) | | ua (User Agent Override) | req.get('User-Agent') or navigator.userAgent or 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)' (defaults to IE6 user-agent for easy filtering) | | dr (Document Referrer) | req.get('Referer') or document.referrer | | dh (Document Host Name) | req.hostname or location.hostname | | dp (Document Path) | req.originalUrl or location.pathname | | an (Application Name) | name value from your project's package.json | | aid (Application ID) | name value from your project's package.json | | av (Application Version) | version value from your project's package.json | | cd1 (Custom Dimension) | name value from your project's package.json | | cd2 (Custom Dimension) | version value from your project's package.json |

For all other GA properties, see Google Analytics Measurement Protocol.

event(eventAction|properties)

Overrides global properties, and fires a custom event to Google Analytics.

eventAction

  • Type: String

Event Action. Equivalent to { action: 'Example' } or { ea: 'Example' }.

properties (GA Measurement Protocol)

  • Type: Object

Defaults:

| Key | Default value | | --------------------------------------------------------------------------------------------------------------- | --------------- | | t (Hit Type) | "event" | | ec (Event Category) | "Primary KPI" |

For all other GA properties, see Google Analytics Measurement Protocol.

Custom properties:

| Key | Type | Description | Maps to | | ------------ | ----------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | clientId | String | A unique representation of the user/session. Its value will be hashed/anonymized. | cid (Client ID) | | priority | Integer (1|2|3) | Resolves to "Primary KPI", "Secondary KPI" or "Tertiary KPI" respectively. | ec (Event Category) | | action | String | Describes the event taking place. | ea (Event Action) | | label | String | Labels the event. | el (Event Label) | | value | Integer | Adds a metric to the label. | ev (Event Value) | | appName | String | Identifies the app name. | an (Application Name), aid (Application ID), cd1 (Custom Dimension) | | appVersion | String | Identifies the app version. | av (Application Version), cd2 (Custom Dimension) |

pageview([properties])

Optionally overrides global properties, and fires a pageview event to Google Analytics.

properties (GA Measurement Protocol)

  • Type: Object

Defaults:

| Key | Default value | | ------------------------------------------------------------------------------------------------------- | ------------- | | t (Hit Type) | "pageview" |