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

@telefonica/google-analytics

v2.0.0

Published

> Google Analytics 4 library for Telefonica web projects

Downloads

859

Readme

@telefonica/google-analytics

Google Analytics 4 library for Telefonica web projects

Usage

import {initAnalytics, setUserProperty, logEvent, setScreenName} from '@telefonica/google-analytics';

initAnalytics('G-XXXXXXXXXX');
setUserProperty('my_user_property', 'my_user_property_value');
setScreenName('my_screen_name');
logEvent({name: 'my_event_name', foo: 'bar'});

Things to know

  • initAnalytics must be called before any other function. This function will inject the gtag script in the document.

  • initAnalytics function is idempotent. Subsequent calls to initAnalytics will not have any effect.

  • logEvent and setScreenName calls won't send any data to Google Analytics until initAnalytics is called. If you call logEvent or setScreenName before initAnalytics, the calls will be queued and sent to Google Analytics once initAnalytics is done.

  • Money amounts must be normalized with sanitizeAnalyticsMoneyAmount function. This function will remove decimals exceeding two decimal places, multiply by 100 and convert the value to a string. If you don't do so and send a number, unexpected results may happen because the normalization described below will be executed before sending the event. This restriction does not apply to ecommerce events (https://support.google.com/analytics/answer/9267735?hl=en).

  • Events are normalized before being sent to Google Analytics:

    • Event params name are truncated to 40 characters.
    • Event params value are truncated to 100 characters.
    • Not allowed characters are removed from event params value.
    • Multiple white spaces are removed and spaces are replaced by underscores in event params value.
    • The same normalization is applied to screen names.
    • For non ecommerce events, numbers are converted to strings and then string normalization is applied. That means if there are dots (as in decimal numbers) they will be removed.
  • If setScreenName is called multiple times with the same screen name, only the first call will be sent to Google Analytics.

  • setScreenName and logEvent return a promise that resolves when the event has been sent to Google Analytics.

  • All the events are automatically filled with a screenName param containing the last screen name set with setScreenName.

Use custom Api

Events are sent to Google servers by default (using gtagApi), but you can use a custom api to change this behavior. For example, Novum app uses the webview bridge to send the events to the native app, and then the native app sends the events to Firebase.

Another use case of custom api is to log events in the browser console in development mode:

import {initAnalytics, consoleApi, gtagApi} from '@telefonica/google-analytics';

initAnalytics('G-XXXXXXXXXX', {
  api: process.env.NODE_ENV === 'production' ? gtagApi : consoleApi,
});

CSP

If you are using a CSP, you must add the following directives for Google Analytics to work:

script-src https://*.googletagmanager.com; connect-src https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com; img-src https://*.google-analytics.com https://*.googletagmanager.com;

Check Google docs for CSP in Google Analytics 4