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 boosts Google Ads event tracking with automatic data collection

Google Tag Manager boosts Google Ads event tracking with automatic data collection

Google
Google

Official Source

Official Source

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

Official Source
Google Tag Gateway launches one-click Google Cloud Platform integration beta

Google Tag Gateway launches one-click Google Cloud Platform integration beta

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 Ads Audit Tool

Top-Notch Google Ads Audit Tool

Featured
Google Tag adds Manage data transmission setting for advanced consent control

Google Tag adds Manage data transmission setting for advanced consent control

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 adds three new Analytics variables and custom Analytics Storage type

Google Tag Manager adds three new Analytics variables and custom Analytics Storage type

Google
Google

Official Source

Official Source

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

Official Source
New in GTM Event Parameters Added for Google Ads Conversion Tracking

New in GTM Event Parameters Added for Google Ads Conversion Tracking

Google Tag Manager Introduces Event Parameters for Ads and Floodlight Tags

Google Tag Manager Introduces Event Parameters for Ads and Floodlight Tags

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
Server-side Google Tag Manager New Release v3.2.0 with Easy Cloud Run Update

Server-side Google Tag Manager New Release v3.2.0 with Easy Cloud Run 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

Related Tools

Markifact logo

Markifact

Verified Tool

Verified Tool

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

Verified Tool

Marketing Workflows Powered by AI

Featured
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