When working with Server-side Google Tag Manager (SGTM) and Firestore, it’s common to write data that should expire automatically after a period. Firestore handles automatic data deletion through TTL (Time To Live) policies, which delete documents once a specified future Date and Time is reached.
However, at the time of writing, no built-in solution allows you to directly set TTL from Server-side GTM. The typical workaround involves initially writing a Timestamp to Firestore and then converting it into a TTL policy with Cloud (Run) Functions.
The Cloud (Run) Functions approach increases Firestore’s read and write operations, potentially leading to higher costs.

How the Firestore Writer with TTL Tag Template Works
Since Server-side GTM Firestore API cannot directly utilize the standard to set TTL, this custom Template sends data directly to the Firestore API using the sendHttpRequest
function. Additionally, because the Server-side GTM API lacks native Date object support, the Template includes its own custom date-math routine to create and manage dates.
Functionality
- Add Event Data: Send all SGTM event data to the Firestore document
- Merge document keys: Merge keys into Firestore documents
- Add Timestamp: Adds timestamp in milliseconds
- Add Time to Live: Adds TTL (Time To Live) Date and Time
Additional Information
Currently, the template isn’t available in the official Google Tag Manager Template Gallery, but you can access and download it directly from GitHub. It’s open source.
The functionality and look of the Tag Template is somewhat similar to the Firestore Writer Tag, and some elements have been borrowed from that Template.
Be the first to comment on "Write TTL (Time To Live) to Firestore using Server-side GTM"