• Stars
    star
    103
  • Rank 323,641 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automated functional testing for Google Tag Manager's Data Layer

gtm-datalayer-test

Automated functional testing for Google Tag Manager's Data Layer.

Requirements

To install and run gtm-datalayer-test, you need the following clients / libraries:

Installation

  1. Clone the repo with git clone https://github.com/sahava/gtm-datalayer-test.git
  2. Run cd gtm-datalayer-test
  3. Run npm install to install the dependencies (run as local user, not root)
  4. Run npm test to run the example script

Ubuntu 16.04 System Setup

First you need to install Node.js, Java, JRE, git, and build-essentials tools.

  1. sudo curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
  2. sudo apt-get install -y nodejs
  3. sudo apt-get install default-jre git
  4. sudo apt-get install -y build-essential

npm test

The npm test script does the following:

  1. Starts a simple HTTP server on http://localhost:8080, with the index.html from ./examples.
  2. Runs the ./examples/spec/basic_example.js spec against the mock dataLayer in index.html
  3. Stops the HTTP server

You can use this script to quickly see how the solution works. Be sure to check both basic_example.js and basic_example.conf.json to understand how the JSON validation works against the actual window.dataLayer object.

Technology

How it works

In examples/spec/basic_example.conf.json (see below), you specify both global key-value pairs as well as page-specific configurations. These dataLayer compositions will then be searched on every page (global configuration) as well as on specific pages (page-specific configurations).

You can enforce the schema in basic_example.conf.json using (a very slightly customized) JSON Schema (v4) to describe the objects in window.dataLayer, or you can use a simple subset check.

###JSON Schema You can describe the window.dataLayer objects using JSON Schema. Each object you add to the dataLayer property in the configuration (global or page-specific), will be compiled into its own schema, and this schema will then be checked against the global window.dataLayer structure. There are some slight modifications and hacks to accomodate for the fact that JSON Schema doesn't play that well with Arrays of indeterminate length and composition (such as window.dataLayer often is).

##Subset check You can also simply use a subset check, where you specify each dataLayer object (global or page-specific) with all the keys and values that you expect to find in dataLayer. Note that if you leave a key out, it is considered optional.

To enable the subset check, you will need to add the key-value pair

"@json" : false

to the root of each dataLayer object in the configuration file that you want to validate against.

Example:

{
    "baseUrl" : "https://www.yourdomain.com",
    "dataLayerName" : "dataLayer",
    "dataLayer" : [{
        "@json" : false,
        "someVariable" : "someValue"
    },{
        "someOtherVariable" : {
            "type" : "number",
            "enum" : [1,2,3,4,5]
        }
    }]
}

Here the first object uses the subset check, so it will validate if the global window.dataLayer structure has at least one object with that exact key-value pair ("someVariable" : "someValue") in the root.

The second object uses the modified JSON Schema. It will validate if at least one object in the global window.dataLayer structure has the root key someOtherVariable set to a number in the range of 1-5.

The JSON Schema used for the test configuration must follow the structure described in this README. When you run the tests, the JSON configuration is first validated against its own schema (/lib/validTestConfSchema.json). If validation fails, the test is aborted.

Customize tests

The test is currently contained in /examples/spec/. There are two files:

  • basic_example.conf.json - the JSON schema that controls on which pages the test visits and what assertations are run against dataLayer
  • basic_example.js - the actual test specification To customize the test, you should modify basic_example.conf.json. Here's what it might look like:
{
    "baseUrl" : "https://www.yourdomain.com",
    "dataLayerName" : "dataLayer",
    "dataLayer" : [{
        "someVariable" : {
            "@rootRequired" : false,
            "type" : "object",
            "properties" : {
                "someDeeperVariable" : {
                    "pattern" : "^deepValue$"
                }
            },
            "required" : ["someDeeperVariable"]
        }
    }],
    "page" : [{
        "path" : "/some-path/",
        "dataLayer" : [{
            "@expect" : "dataLayer to have Enhanced Ecommerce detail object",
            "ecommerce" : {
                "type" : "object",
                "properties" : {
                    "detail" : {
                        "$ref" : "/enhancedEcommerceSchema.json#/definitions/detail"
                    }
                },
                "required" : ["detail"]
            }
        }]
    }]
}

These are all the keys you can currently use:

Property Type Description
baseUrl (required) URI Base URL for tests, no trailing /.
multipleContainers Boolean If true then multiple instances of 'gtm.js' event are permitted in dataLayer.
dataLayerName String The name of the global window.dataLayer Array. Make sure this matches what you've configured in the Google Tag Manager container snippet. It defaults to dataLayer.
dataLayer Array Global dataLayer configuration. An Array of objects that you expect to find on every page of the site. The objects can be incomplete, as only the named key-value pairs will be searched for.
page Array Array of page-specific configurations, where each object corresponds to a page you want to test.
page.path (required) String Path of the page you want to test.
page.dataLayer (required) Array Array of objects, where each object is a single test run against dataLayer. Each object can have any number of key-value pairs, and only these key-value pairs are searched for (so the objects can be incomplete). The objects should correspond with what you expect to find in dataLayer.
page.dataLayer[].@expect (required) String A description of the test. It will be shown in the test reporter output.
(page.)dataLayer[].properties{}.key.@rootRequired Boolean If set to false will make this root-level property optional, meaning the test will pass even if the key is not found in the global dataLayer structure.

If you use the JSON Schema option (by default), everything within the dataLayer objects (global and page-specific) must conform to the latest draft of JSON Schema (http://json-schema.org/). The only exceptions are:

  • The root level of each dataLayer object is translated directly into properties of respective root level window.dataLayer objects. So instead of specifying the root level dataLayer objects in the configuration as complete JSON Schema objects, just list the properties as keys. Everything else (except for the other exceptions listed below) should be described using the regular JSON Schema specification.
  • @expect used in the page-specific configurations to describe the current test for the test reporter. This is required also when using the subset check method.
  • @rootRequired used in root-level properties to indicate whether or not they are optional in the global dataLayer structure.

To do

  • Add support for navigation (e.g. validate dataLayer after user interactions).
  • Improve the configuration to make it easier to use different browser drivers.

More Repositories

1

GoogleTagManagerTemplates

Custom templates for Google Tag Manager
Smarty
189
star
2

spam-filter-tool

Spam Filter Insertion Tool
JavaScript
111
star
3

eec-gtm

DOM scraping scripts for tracking content with Enhanced Ecommerce with Google Tag Manager
JavaScript
83
star
4

multisite-lighthouse-gcp

Run Lighthouse audits on URLs, and write the results daily into a BigQuery table.
JavaScript
46
star
5

multisite-lighthouse

Create lighthouse reports for multiple sites
JavaScript
38
star
6

datalayer-typescript

TypeScript definitions for Google Tag Manager's dataLayer
TypeScript
36
star
7

web-scraper-gcp

Scrape all the pages and links of a given domain and write the results to Google Cloud BigQuery.
JavaScript
34
star
8

GoogleTagManagerTemplates_site

gtm templates dot com site
JavaScript
26
star
9

ga-cl

Google Analytics API Command Line Tools
Python
25
star
10

cookie-bouncer-service

App Engine service that creates a POST API endpoint which sets the Set-Cookie header based on request data.
JavaScript
24
star
11

sgtm-cloud-run-shell

Shell script for deploying Server-side Google Tag Manager with Google Cloud Run.
Shell
22
star
12

readability-score-javascript

Simple vanilla JavaScript rewrite of the textstat.py library.
JavaScript
21
star
13

ga-custom-dimension-manager

Custom Dimension Manager for Google Sheets
HTML
21
star
14

ga-validator-apps-script

Google Analytics Custom Dimension validator built in Apps Script
JavaScript
18
star
15

server-side-gtm

Documentation for configuring, deploying, and running server-side Google Tag Manager applications.
18
star
16

content

Advanced Content Tracking by Justin Cutroni for GTM
JavaScript
17
star
17

gtm-tools-sheets-documentation

GTM documentation tool for Google Sheets
JavaScript
12
star
18

gtm-slack-integration

Google Cloud Functions setup to send a Slack message when a container version is published
JavaScript
11
star
19

gtm-hierarchy-sheets

Simple Google Sheets add-on to fetch your GTM hierarchy
JavaScript
10
star
20

gtm-api-to-export-json

Steps to take when translating a Google Tag Manager API "Version" resource into the import JSON format.
10
star
21

sahava.github.io

HTML
10
star
22

ga-gtm-validator

Validates GA Custom Dimensions and GTM tags across a given set of GA properties and containers
Python
7
star
23

good-conversion-rate

HTML
5
star
24

gtm2txt

gtm2txt
Python
5
star
25

gtm-cl

Google Tag Manager Command Line Tools
Python
2
star
26

gtm-sonar

Source files for the GTM Sonar Chrome extension
JavaScript
2
star
27

leaving-earth-outcomes

Outcomes management app for the board game Leaving Earth
JavaScript
1
star