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

@advanced-rest-client/app-analytics

v3.0.1

Published

An element that support Google Analytics analysis

Downloads

26

Readme

Published on NPM

Build Status

Published on webcomponents.org

app-analytics

Event based component to support Google Analytics measurement protocol.

The component support both imperative and declarative API so it can be used in regular HTML template using your favourite templating engine and in JavaScript.

The appname and trackingid attributes (appName and trackingId properties respectively) are required parameters. Without it the element won't initialize a call to GA endpoint.

If clientid attribute (clientId property) is not set then it generates one automatically and store is in localStorage. Note, that if current environment does not support localStorage (like Chrome Apps) you need to provide this property manually or otherwise it will be regenerated each time the user opens the app.

Note, Google Analytics does not allow sending any information that may lead to a user.

Always give the user ability to disable tracking. Under EU laws you need to have permission from the user to store data on local device. To disable analytics simply remove the element from the DOM or set disabled property.

Note, the disabled state is persistent in localStorage and automatically restored when element is initialized. If the environment does not support localStorage you need to set this attribute manually each time the element is initialized.

Using events API

You can directly call one of send*() functions. See API Reference below for more info.

  • sendEvent
  • sendException
  • sendScreen
  • sendSocial
  • sendTimings

You can also use HTML events to send a hit. In this case dispatch a send-analytics event with required type property on the detail object which describes what king of hit should be send. Possible values are: pageview, screenview, event, social, exception or timing.

Other parameters depends on the type.

Sending screenview hit

const event = new CustomEvent('send-analytics', {
 bubbles: true,
 composed: true,
 detail: {
   type: 'screenview',
   name: 'Some scree name' // required
 }
});
document.body.dispatchEvent(event);

Sending event hit

const event = new CustomEvent('send-analytics', {
 bubbles: true,
 composed: true,
 detail: {
   type: 'event',
   category: 'Some category', // required.
   action: 'Some action', // required.
   label: 'Some label',
   value: 123
 }
});
document.body.dispatchEvent(event);

Sending exception hit

const event = new CustomEvent('send-analytics', {
 bubbles: true,
 composed: true,
 detail: {
   type: 'exception',
   description: 'Exception description', // required.
   fatal: true, // default false
 }
});
document.body.dispatchEvent(event);

Sending social hit

const event = new CustomEvent('send-analytics', {
 bubbles: true,
 composed: true,
 detail: {
   type: 'social',
   network: 'Facebook', // required.
   action: 'Share', // required
   target: 'https://www.shared.com/resource' // required
 }
});
document.body.dispatchEvent(event);

Sending timing hit

const event = new CustomEvent('send-analytics', {
 bubbles: true,
 composed: true,
 detail: {
   type: 'timing',
   category: 'Bootstrap', // required.
   variable: 'databaseInitTime', // required
   value: 123, // required
   label: 'Optional label'
 }
});
document.body.dispatchEvent(event);

Custom metrics and dimensions

Use <app-analytics-custom> element as a child of <app-analytics> to set custom properties. This metrics / dimensions will be used with every hit as long as this elements exists as a children of the <app-analytics> element.

Example

<app-analytics trackingid="UA-XXXXXXX">
 <app-analytics-custom type="metric" index="1" value="5"></app-analytics-custom>
</app-analytics>

To send custom data with single hit only without creating <app-analytics-custom> children, add customDimensions or customMetrics to the event detail object. Both objects must be an array of custom definition objects that includes index and value.

Example

const event = new CustomEvent('send-analytics', {
 bubbles: true,
 composed: true,
 detail: {
   type: 'event',
   category: 'Engagement',
   action: 'Click',
   label: 'Movie start',
   customDimensions: [{
     index: 1, // index of the custom dimension
     value: 'Author name' // Value of the custom dimension
   }]
 }
});
document.body.dispatchEvent(event);

Usage

In a HTML page

<script type="module" src="@advanced-rest-client/app-analytics/app-analytics.js"></script>
<script type="module" src="@advanced-rest-client/app-analytics/app-analytics-custom.js"></script>

<app-analytics trackingid="UA-XXXXXXX">
 <app-analytics-custom type="metric" index="1" value="5"></app-analytics-custom>
</app-analytics>

In a LitElement template

import { LitElement, html } from 'lit-element';
import '@advanced-rest-client/app-analytics/app-analytics.js';
import '@advanced-rest-client/app-analytics/app-analytics-custom.js';

class SampleElement extends LitElement {
  render() {
    return html`
    <button @click="${this._handler}">Click with action</button>

    <app-analytics trackingid="UA-XXXXXXX">
     <app-analytics-custom type="metric" index="1" value="5"></app-analytics-custom>
    </app-analytics>
    `;
  }

  _handler(e) {
    this.shadowRoot.querySelector('app-analytics').sendScreen('Main screen');
  }
}
customElements.define('sample-element', SampleElement);

Imperative use

<app-analytics trackingid="UA-XXXXXXX">
 <app-analytics-custom type="metric" index="1" value="5"></app-analytics-custom>
</app-analytics>
<script>
{
  document..querySelector('app-analytics').sendScreen('Main screen');
}
</script>

New in version 3

  • Dropped support for Polymer library. It is now a plain web component.
  • Added aria-hidden attribute.
  • Removed internal recognition of offline mode. Use offline property instead to enable queueing for events to be send when back online.

Development

git clone https://github.com/@advanced-rest-client/app-analytics
cd app-analytics
npm install

Running the demo locally

npm start

Running the tests

npm test