Stape
Search
Try for free

Write data to Firestore from server Google Tag Manager

Updated
Nov 13, 2024
Published
Apr 26, 2022
Also available in

Firestore and sGTM integration give numerous opportunities for server-side tagging. The best thing you can do with this combination is to enrich server-side data. 

You can quickly get data from Firestore using a new Firestore Lookup variable. It’s a native variable and available for everyone in sGTM. But how can you write data to Firebase? Stape solved this problem by creating a Firestore Writer tag. 

This blog post will show how to write data to Firestore from the server Google Tag Manager container. I’ll also show how to use the Firestore Writer tag and Firestore Lookup variable to enrich server-side data. Let's get started! 

What is Firestore

Firestore is a flexible and scalable NoSQL database with document-oriented storage. Unlike SQL databases, there are no tables or rows in Firestore - instead, you store data into documents that get organized into collections for easy navigation and management.

firestore server Google Tag Manager

Firestore is the perfect solution for those with extensive collections of small documents. It stores data in documents that are organized in collections. Each document includes name-value pairs. 

Firestore and server Google Tag Manager

With the newest release of server-side GTM API, it’s now possible to build integration of Firestore and sGTM. We used this API to create the Firestore Writer tag. Let me quickly describe what functions Firestore sGTM API gives. 

Firestore.read - reads data from Firestore document and returns a promise that resolves to an object containing two keys: id and data. 

Firestore.write - writes data to a Firestore document or collection. If you specify only the path to the collection, the document will be automatically created with a random name. 

Firestore.query - returns an array of Firestore documents that match specified query conditions.

Firestore.runTransaction - allows reading and writing from Firestore automatically.

Firestore Writer tag

firestore writer tag for server Google Tag Manager

Firebase Path - specify a path to your Firestore document or collection. Must not start or end with a '/.' If the path is to a collection, a document will be created with a randomly generated ID. If the path is to a document that does not exist, it will be generated.

There are several action types that this tag allows:

  1. Add Event Data - send all Event Data you have in sGTM to the Firestore document. If Event Data and Custom Data have the same field, the Custom Data value will be used for that field. In other words, you can override Event Data values with Custom Data. 
  2. Merge document keys - If checked, the tag will merge keys in the Fire Store document with the data from sGTM. Otherwise, the tag will override the whole document.
  3. Override Firebase Project ID - The projectId is retrieved from the environment variable GOOGLE_CLOUD_PROJECT as long as the access_firestore permission setting for the project ID is set to * or GOOGLE_CLOUD_PROJECT. If the server container runs on Google Cloud, GOOGLE_CLOUD_PROJECT will already be assigned to the Google Cloud project's ID.
  4. Add Timestamp - when enabled, the tag will add the millisecond timestamp to the document.

Custom Data - use this to override event data.

To set up the Firestore Writer tag you will need

If you use stape for sGTM hosting, you will need these things to set up the Firestore Writer tag:

1. Google Service Account connected to stape

1. Create or login to the Google Cloud Platform web console.

2. Select IAM & Admin → Service Accounts → Click Create service account.

Create Google service account.

3. Add account → Click Next → Select Roles BigQuery Data Editor role for BigQuery access or the Cloud Datastore User role for Firestore. 

If you want to use Google Service Account only for BigQuery, choose only the BigQuery Data Editor role. The same for Firestore. 

When you get to the 3rd step, just click Done. 

select Google service account roles

4. Create private key → Select JSON → select Create → JSON will be downloaded to your computer. 

generate Google service account private key

5. Open your stape.io account → open sGTM container → open Power-ups tab → find "Google Service Account" power-up → Upload JSON file that you’ve downloaded from Google Cloud → Click Save. 

upload Google service account private key to stape

2. Firebase account

1. Open Firebase → click Create Project → Select the same project you’ve used to create Google Service Account → click Continue → Click Add Firebase on the third step.     

2. Click Build → Firestore Database

create firebase project

3. Click Create Database → select Start in production mode → Click Next.

create firebase database

4. Select Cloud Firestore location. It should match your sGTM server location. To check the sGTM server location, go to your stape.io account → open the sGTM container that you want to integrate with Firebase and check Server Location.  

Select Cloud Firestore location

5. Click Start a collection. Here you will need to set Firebase Path that lately will be used for Firestore Writer Tag. I’ve added Firebase Path to purchase → Click Save.

Click Start firestore collection

How to set up the Firestore Writer tag

The ability to write and read data to/from Firestore gives numerous scenarios for data enrichment. You store data in Firestore using the Firestore Writer tag and extract the needed data with the help of the Firestore Lookup variable to send it in sGTM tags. 

In this example, I want to send user data to Facebook conversion API and when this information is not available in sGTM event data. To achieve it, I will store user data and Google Analytics client id (_gid) in Firebase when a user makes a purchase. Using the Firestore lookup variable and _gid (as a user's unique identification), I will extract user details on the page_view event and enrich the Facebook CAPI pageview event.

2. Send data to the server GTM container. The two most popular sending data to sGTM are Google Analytics 4 and Data Tag/Data Client.

3. Download Firestore Writer tag from GitHub or add it from the sGTM template Gallery → Open templates sections in the server Google Tag Manager container → Click New.

4. Click three dots in the top right corner → Click Import → Select the Firestore Writer tag template you’ve recently downloaded from GitHub → Click save.

import Firestore Writer tag

5. Create a new Firestore Writer tag → Add Firebase Path. I will use path purchase, this setup will create a new collection called purchase in Firestore, and all new purchases will be added as Firestore documents with random names. I will select an action Add Event Data, which means that the tag will write all event data to Firestore. I also added Custom Data to add user_email and user_id to the Firestore document. Since this is a test site and demo setup, I would not hash user data, but if you use a similar format on production, make sure to hash user data.

Create a new Firestore Writer tag

6. Test your tag and check what data was added to Firestore. In my case, whenever a user purchases on the site, a new document is added to Firestore with sGTM event data. 

Test Firestore writer tag

7. Now, create the Firestore Lookup variable in sGTM. I selected Collection Path & Query → Specify Collection Path → Set your condition. In my case, every time user_id (in Firestore) exactly matches the gid variable (in sGTM), the lookup variable will extract the user_email value in the corresponding Firestore document.

create the Firestore Lookup variable in sGTM

8. Test Firestore Lookup variable in sGTM preview. 

Test Firestore Lookup variable in sGTM preview

9. Now, I can send enriched sGTM data with user email even if user email is not available in sGTM. 

Conclusion

The possibilities are endless when it comes to data enrichment with the help of Firestore and server Google Tag Manager. It’s relatively easy to write sGTM data to Firestore. The primary step is to create a scheme of data stored in Firestore and extract it when you need it to enrich sGTM tags.

If you need help setting up server-side tracking, don't hesitate to contact us. We can help you get your data into Firestore and start deriving insights from it in no time. Thanks for reading!

Try Stape for all things server-sideright now!