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

@d-cat/tag-template-google-analytics-ecommerce

v5.1.0

Published

D-CAT Google Analytics Ecommerce Tag Template.

Downloads

17

Readme

Tag Template: Google Analytics Ecommerce

codecov

Tag template to initiate Google Analytics Enhanced Ecommerce. It's designed to use together with @d-cat/tag-manager.

Install

npm i @d-cat/tag-template-google-analytics-ecommerce

Usage

The Ecommerce class returns the main functionality of the template.

  • It handles a given object to send data to Google Analytics, including product / user scoped dimensions.
  • You can create 1 Ecommerce-object per Tracker, since it's likely that each tracker has a specific configuration.
  • It is assumed that the Google Analytics Main Tag already has ga(TRACKERNAME.ec:require) activated when using the ecommerce tag.
  • It is also assumed that the ecommerce-data is according to the JSON-schemas DCAT provides via Confluence.

Configuration

import Ecommerce from '@d-cat/tag-template-google-analytics-ecommerce';

// Default setup without Custom Dimensions/Metrics
const ecommerce = new Ecommerce({
  optionsToSet: {},
  trackername: 'main',
});

// Personalized setup with Custom Dimensions
const config: models.IConfig = {
  optionsToSet: {
    // hit scoped dimensions/metrics linked to only 1 event, taken from the event-data-payload
    setHit: async payload => {
      return {
        dimension47: payload.payment_method,
      };
    },
    // dimensions/metrics that you wish to set once for all hits to come on this page.
    // these are set on initialization, so don't work with ecommerce-data-payloads.
    setOnce: async () => {
      return {};
    },
    // dimensions/metrics that you wish to set on product-level. These are generated on item/product-level.
    // preferably these are in the details-object on product level.
    setProduct: async item => {
      return {
        dimension36: item.details?.binding_period,
      };
    },
  },
  // trackername
  trackername: 'myTrackerName',
};

Activation

This is done via the render-method, where the data-payload is passed to the ecommerce-tag, like this:

subscribe({
  event: 'ecommerce.*',
  handler: ({ data }: any) => {
    ecommerce.render(data);
  },
  id: 'tdn2010: tag - google analytics - ecommerce tag',
});
  • In the ecommerce-tag is the switch-statement below that fires an ecommerce based on the action in the data-payload. So you don't have to worry about this part.
switch (data.action) {
        case 'detail':
          return this.detail(data as models.IDetail, ecommerceObj, setProduct, setHit);

        case 'listing':
          return this.listing(data as models.IListing, ecommerceObj, setProduct, setHit);

        case 'add_to_cart':
          return this.addToCart(data as models.IAddToCart, ecommerceObj, setProduct, setHit);

        case 'remove_from_cart':
          return this.removeFromCart(data as models.IRemoveFromCart, ecommerceObj, setProduct, setHit);

        case 'checkout':
          return this.checkout(data as models.ICheckout, ecommerceObj, setProduct, setHit);

        case 'purchase':
          return this.purchase(data as models.IPurchase, ecommerceObj, setProduct, setHit);

        default:
          throw new Error('Given ecommerce-action does not validate.');
      }
    } catch (err) {
      throw new Error(`Error during rendering Google Analytics Ecommerce: ${err}`);
    }

Advanced Setup

When the Ecommerce class doesn't satisfy your needs, you can inherit certain logic and override it with your own.

  • Below is a decription of all the functional methods in the ecommerce-tag, that could you could override.
  • All method return an async function that adds the products to the action, sets the action and sends the hit.
  • These functions are async, since we want to make sure Custom Dimensions are sending the latest value of a variable.
import EnhancedEcommerce from '@d-cat/tag-template-google-analytics-ecommerce';

export default class MyClass extends EnhancedEcommerce {
  constructor(...args: any[]) {
    super(...args);
  }

  listing(...args: any[]) {
    super.listing(...args);
    // here you can add your own logic.
  }
}

detail(data:IDetail): Promise<T>

Builds and sends a hit to Google Analytics based specific for detail events.

import { IData, IDetail, IFieldsToSet, IVodafoneProduct, IZiggoProduct} from '@d-cat/tag-template-google-analytics-ecommerce';
ecommerce.detail<T>(
    data: models.IDetail,
    ecommerceObj: models.IData,
    setProduct: (item: Partial<models.IZiggoProduct | models.IVodafoneProduct>) => Promise<models.IFieldsToSet>,
    setHit: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
  )

listing(data:IListing): Promise<T>

Builds and sends a hit to Google Analytics based specific for listing events.

import { Data, IFieldsToSet, IListing, IVodafoneProduct, IZiggoProduct } from '@d-cat/tag-template-google-analytics-ecommerce';
ecommerce.listing<T>(
    data: models.IListing,
    ecommerceObj: models.IData,
    setProduct: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
    setHit: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
  )

addToCart(data:IAddToCart): Promise<T>

Builds and sends a hit to Google Analytics based specific for add to cart events.

import { IAddToCart,IData, IFieldsToSet, IVodafoneProduct, IZiggoProduct } from '@d-cat/tag-template-google-analytics-ecommerce';
ecommerce.addToCart<T>(
    data: models.IAddToCart,
    ecommerceObj: models.IData,
    setProduct: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
    setHit: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
  )

removeFromCart(data:IRemoveFromCart): Promise<T>

Builds and sends a hit to Google Analytics based specific for remove from cart events.

import { IRemoveFromCart, IData, IFieldsToSet, IVodafoneProduct, IZiggoProduct} from '@d-cat/tag-template-google-analytics-ecommerce';

ecommerce.removeFromCart<T>(
    data: models.IRemoveFromCart,
    ecommerceObj: models.IData,
    setProduct: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
    setHit: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
  )

checkout(data:ICheckout): Promise<T>

Builds and sends a hit to Google Analytics based specific for checkout events.

import { ICheckout, IData, IFieldsToSet, IVodafoneProduct, IZiggoProduct } from '@d-cat/tag-template-google-analytics-ecommerce';
ecommerce.checkout<T>(
    data: models.ICheckout,
    ecommerceObj: models.IData,
    setProduct: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
    setHit: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
  )

purchase(data:IPurchase): Promise<T>

Builds and sends a hit to Google Analytics based specific for checkout events.

import { IPurchase } from '@d-cat/tag-template-google-analytics-ecommerce';

// when using typescript the Purchase type is needed.
ecommerce.purchase<T>(
    data: models.IPurchase,
    ecommerceObj: models.IData,
    setProduct: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
    setHit: (data: Partial<models.IData>) => Promise<models.IFieldsToSet>,
  )