Skip to content

O-Pall/SGTM-Variable__Firestore-Get-Full-Document

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

sGTM Variable Template: Firestore Get Full Document

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.

🚀 Why use this?

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.

✨ Key features

  • 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 in operator 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.

⚙️ Configuration

1. Global settings

  • Google Cloud Project ID : Your GCP Project ID.
  • Collection : The path to your Firestore collection (e.g., products, users).

2. Query types

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.

📦 Output

  • Single match : Returns a JavaScript Object of the document.
  • Batch / Multiple match : Returns an Array of JavaScript Objects.

🛠️ Installation

  1. Download the template.tpl file.
  2. In sGTM, go to Templates > New > Import.
  3. Select the file, save, and publish.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published