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 Introduces Automated Cloudflare Setup for First-Party Mode

Google Tag Manager Introduces Automated Cloudflare Setup for First-Party Mode

Google for Developers
Google for Developers

Official Source

Official Source

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

Official Source
Google Tag UI Enhances Platform with Integrated Consent Management Features Trending ️‍πŸ”₯

Google Tag UI Enhances Platform with Integrated Consent Management Features

Ginny Marvin
Ginny Marvin

Official Source

Official Source

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

Official Source
Google Tag Manager Updates Conversion Linker with Local Storage Support

Google Tag Manager Updates Conversion Linker with Local Storage Support

Google
Google

Official Source

Official Source

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

Official Source
The Ultimate Google Analytics Audit Tool

The Ultimate Google Analytics Audit Tool

Featured
Google Tag Manager Launches Server-Side Tagging Support for Mobile Apps Trending ️‍πŸ”₯

Google Tag Manager Launches Server-Side Tagging Support for Mobile Apps

Brais Calvo
Brais Calvo

Top Creator

Top Analytics Creator

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

Top Analytics Creator
Google Adds 2 New Alerts to Tag Diagnostics Tool

Google Adds 2 New Alerts to 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
Google Tag Manager Introduces First-Party Mode Deployment via Cloudflare

Google Tag Manager Introduces First-Party Mode Deployment via Cloudflare

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 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

Related Tools

Marketing Auditor logo

Marketing Auditor

Verified Tool

Verified Tool

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

Verified Tool

Automated audits for Google Ads and Analytics.

Get Featured Here

Showcase your tool in this list.

Contact Us