Coder Social home page Coder Social logo

puppeteer-datalayer's Introduction

puppeteer-datalayer

puppeteer-datalayer is a node.js module to facilitate interaction with Google Tag Manager's dataLayer in combination with Puppeteer.

What's it for?

puppeteer-datalayer was built with the following two main use cases in mind.

Especially when used with other testing tools like Jest and joi it can be a huge timesaver, because you won't have to spend all your time manually clicking through your site to check if your tracking implementation is doing what it's supposed to.

Frontend Testing

Puppeteer is very useful when doing frontend testing, i. e. checking whether your dataLayer events are trigger properly upon certain user actions on the website, e. g. adding a product to the shopping cart. Checking if the expected event is pushed to dataLayer after mocking the user action is a lot easier with puppeteer-datalayer.

GTM Testing

Make sure your tags in GTM work as expected by pushing predefined events to dataLayer. Afterwards you can listen to network requests in Puppeteer to see if the expected requests (Google Analytics, 3rd party conversion tags, โ€ฆ) were triggered and contain the necessary parameters.

Installation

You can install this package from npm by executing the following command while in your project directory:

npm install puppeteer-datalayer --save

Glossary

To clarify the terminology used in this package:

Message

A message describes any object inside dataLayer regardless of its attributes. These are examples of messages pushed to dataLayer:

dataLayer.push({ event: "addToBasket", product: { id: 12345 } })
dataLayer.push({ order: null })
dataLayer.push({})

Event

An event describes any object inside dataLayer that has an event attribute. An event is always a message but not every message is an event. The name applies no matter whether an event was pushed by a developer-implemented dataLayer.push() or the automatically collected events starting with gtm..

// This is an event
dataLayer.push({ event: "gtm.click" })

// This one, too
dataLayer.push({ event: "addToBasket", product: { id: 12345 } })

// This is not an event, just a message
dataLayer.push({ order: null })

Credits

Special thanks go out to the following people whose content represents the basis this package was built on. Check out their awesome resources:

API

Table of Contents

constructor

Instantiates the dataLayer class

Parameters

  • page string The page object as provided by puppeteer
  • containerId string The containerId of the GTM container you want to interact with

get

Gets the current value of the specified dataLayer variable

Parameters

Returns Promise Promise that resolves to the current value of the variable

getContainerIDs

Returns all active container IDs for the page

Returns array An array of strings containing all container IDs

getDataModel

Returns the the full data model, i. e. the current names and values of all variables in dataLayer

Parameters

  • containerId (optional, default this.containerId)
  • The string GTM Container ID to fetch the data model from. Defaults to the containerId defined on the instance

Returns Promise A Promise that resolves to the data model of dataLayer as an object

getEvents

Returns all dataLayer events matching the given event name

Parameters

Returns array An array of event objects

getLatestEvent

Returns the most recent event matching the specified event name from dataLayer. Simply returns the most recent event if no eventName is specified

Parameters

  • eventName string The event name to match

Returns Promise A Promise that resolves to the latest da event

getLatestMessage

Returns the most recent message from dataLayer

Returns Promise A Promise that resolves to the latest dataLayer message

history

Retrieves all messages that are present in dataLayer

Returns Promise A Promise that resolves to the full dataLayer array

push

Pushes a message (which can be an event) to the dataLayer

Parameters

  • message object The message to push

waitForEvent

Waits for the specified dataLayer event

Parameters

Returns promise A promise that resolves as soon as the event happens

puppeteer-datalayer's People

Contributors

justusbluemer avatar williamjulianvicary avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

puppeteer-datalayer's Issues

Typo was not fixed in NPM

Hi there! Thanks for your work ๐Ÿ‘

I've just did npm install to the package, got version 1.0.1, and still have this typo in the code b885c55

I had to debug my way there to understand why things are not working :) Would you mind pushing a new version to NPM?

Screen Shot 2020-10-01 at 9 51 01 AM

Do you have any examples?

Do you have any examples or some getting started boilerplate code? I have no idea how to start implementing this. Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.