Google Tag Manager Adds mockObject API for Custom Template Testing

August 04, 2024 at 8:23:57 AM

TL;DR Google Tag Manager (GTM) has updated its custom template APIs with a new mockObject feature, enabling developers to write unit tests for complex APIs like Firestore and LocalStorage. The mockObject API overrides Sandboxed APIs that return objects, operates only in test mode, and automatically resets before each test run. Using the syntax mockObject(apiName, objectMock), it enhances testing accuracy, debugging, and reliability for custom templates.

Google Tag Manager Adds mockObject API for Custom Template Testing

Google Tag Manager (GTM) has updated its custom template APIs with a new mockObject feature. This addition allows developers to write unit tests for complex APIs that return objects, such as Firestore and LocalStorage.

Key Features of the mockObject API

  1. Object Mocking: Overrides the behavior of Sandboxed APIs that return objects.
  2. Test Mode Exclusive: The API only operates in test mode.
  3. Automatic Reset: Mocks reset before each test run.

How It Works

The mockObject API uses this syntax:

mockObject(apiName, objectMock);

  • apiName (string): The API name to mock, matching the string passed to require()
  • objectMock (object): The value or function to return for the API

Benefits for Developers

  1. Testing Accuracy: Tests can use actual template code instead of dummy values.
  2. Debugging: Precise mocking helps identify issues in complex templates.
  3. Reliability: Comprehensive testing leads to more dependable custom templates.

Example Use Case

Here's an example demonstrating how to simulate write and read operations for Firestore in tests:

const storage = {};
let firestoreId = 1;

function asTestPromise(result) {
  return {
    then: (callback) => callback(result)
  };
}

mockObject('Firestore', {
  write: (collection, input) => {
    storage[collection + '/' + (++firestoreId)] = input;
    return asTestPromise(firestoreId);
  },
  read: (document) => asTestPromise({data: storage[document]})
});

This addition to Google Tag Manager's testing capabilities allows for more thorough testing of custom templates, particularly those using complex APIs that return objects. An example of mocking the Firestore API:

Have more questions on this topic? Ask our AI assistant for in-depth insights.

Read more from sources πŸ‘‡

Simo Ahava
Simo Ahava

Top Creator

Top Analytics Creator

Simo Ahava is a Top Analytics Creator. Part of Swipe Insight Select, a curated list of top creators.

Top Analytics Creator
- Google Tag Manager (GTM) has updated its custom template APIs with a new mockObject feature, enabling developers to writ... Visit Source Open external source URL

The Only Digital Marketing Feed You'll Ever Need.

Stay informed your way. Tailored updates when and how you want them. 100% Free.

10,000+ Users

500+ Sources

1000+ Tools

Or

Related Posts

Google Tag Manager Server-Side Introduces Automatic GA4 Configuration Fetching

Google Tag Manager Server-Side Introduces Automatic GA4 Configuration Fetching

Simo Ahava
Simo Ahava

Top Creator

Top Analytics Creator

Simo Ahava is a Top Analytics Creator. Part of Swipe Insight Select, a curated list of top creators.

Top Analytics Creator
Top-Notch Google Analytics Audit Tool

Top-Notch Google Analytics Audit Tool

Sponsored
GA4 Auditor
GA4 Auditor

Verified Sponsor

Verified Sponsor

GA4 Auditor is a Verified Sponsor. Want to get featured here? Contact us.

Verified Sponsor
Google Expands Tag Diagnostics and Introduces Integrated Consent Management Setup Trending ️‍πŸ”₯

Google Expands Tag Diagnostics and Introduces Integrated Consent Management Setup

Google
Google

Official Source

Official Source

Google is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Tag Updates Consent Mode Parameter for Colorado Privacy Settings

Google Tag Updates Consent Mode Parameter for Colorado Privacy Settings

Simo Ahava
Simo Ahava

Top Creator

Top Analytics Creator

Simo Ahava is a Top Analytics Creator. Part of Swipe Insight Select, a curated list of top creators.

Top Analytics Creator
Google Launches Pantheon to Enhance Server-Side Google Tag Manager Capabilities

Google Launches Pantheon to Enhance Server-Side Google Tag Manager Capabilities

Christoph Scherf
Christoph Scherf

Official Source

Official Source

Christoph Scherf is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Server-Side GTM Users: No Action Needed for Google Cloud OpenSSH Update

Server-Side GTM Users: No Action Needed for Google Cloud OpenSSH Update

Simo Ahava
Simo Ahava

Top Creator

Top Analytics Creator

Simo Ahava is a Top Analytics Creator. Part of Swipe Insight Select, a curated list of top creators.

Top Analytics Creator
Google Tag Manager to End Internet Explorer Support on July 15

Google Tag Manager to End Internet Explorer Support on July 15

Google
Google

Official Source

Official Source

Google is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Tag Manager Rolls Out Tag Diagnostics Tool Trending ️‍πŸ”₯

Google Tag Manager Rolls Out Tag Diagnostics Tool

Google
Google

Official Source

Official Source

Google is a Official Source. The source has been verified by Swipe Insight team.

Official Source

Related Tools

GA4 Auditor logo

GA4 Auditor

Verified Tool

Verified Tool

GA4 Auditor is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Automated GA4 audits with actionable insights

Get Featured Here

Showcase your tool in this list.

Contact Us