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!
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 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.
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.
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:
Custom Data - use this to override event data.
If you use stape for sGTM hosting, you will need these things to set up the Firestore Writer tag:
1. Create or login to the Google Cloud Platform web console.
2. Select IAM & Admin -> Service Accounts -> Click Create 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.
4. Create private key -> Select JSON -> select Create -> JSON will be downloaded to your computer.
5. Open your stape.io account -> open sGTM container -> open Power-ups tab -> Upload JSON file that you’ve downloaded from Google Cloud -> Click Save.
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.
3. Click Create Database -> select Start in production mode -> Click Next.
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.
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.
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.
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.
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.
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.
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.
8. 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.
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!
All it takes is a few simple questions. Click Get A Quote, fill-up the form, and we will send you a quote.