This Server-Side Google Tag Manager (sGTM) variable template lets you pull entire Firestore documents in just one request. It uses smart batching to drastically cut down on read operations, saving you latency and cloud costs compared to standard templates.
Most Firestore templates only fetch one field per request. If you need to enrich a list of items—like 4 products in a GA4 view_item_list—with multiple details (Price, Category, Variant), standard templates will flood your database with read requests.
Comparison :
- Standard templates : 4 Products × 4 Attributes = 16 Requests
- This template : 4 Products = 1 Request (or 4 batched lookups)
Impact : You could slash your Firestore read quotas and costs by a significant amount (depends on setups but at least 50% from the moment you fetch more than 2 fields per request) for heavy data enrichment pipelines.
- Full document retrieval : Gets the complete JSON object, not just a single key.
- Smart batching : Automatically groups multiple values (like a comma-separated string of IDs) using Firestore's
inoperator to keep requests to a minimum. - Dual query modes :
- Document : Direct lookup using the Document ID.
- Field : Lookup based on a specific field value (e.g.,
sku,transaction_id).
- Deduplication : Automatically cleans up duplicate input values before querying so you don't waste resources.
- Google Cloud Project ID : Your GCP Project ID.
- Collection : The path to your Firestore collection (e.g.,
products,users).
| Type | Description | Inputs Required |
|---|---|---|
| Document | Retrieve a single document by its specific Firestore ID. | Document ID |
| Field | Retrieve document(s) where a specific field matches your value(s). Supports batching. | Field Name, Field Value, Delimiter (optional) |
Note on batching : To use batching in Field mode, just provide a string of values separated by a delimiter (like ID_A,ID_B,ID_C) and define that delimiter in the configuration.
- Single match : Returns a JavaScript Object of the document.
- Batch / Multiple match : Returns an Array of JavaScript Objects.
- Download the
template.tplfile. - In sGTM, go to Templates > New > Import.
- Select the file, save, and publish.