Berkay Demirbaş
Berkay Demirbaş - 24 May 2024
Head of Product Analytics

Server-side Google Tag Manager (sGTM) offers enhanced flexibility and security for tracking and handling data in your e-commerce applications. One powerful application of sGTM is writing purchase data directly to Firestore, Google Cloud's NoSQL database.

This blog post will walk you through the process of setting up sGTM to capture e-commerce purchases and store them in Firestore.

Step 1: Creating a New Server-side Google Tag Manager (sGTM) Template

In this step, you'll create a custom template in your server-side Google Tag Manager (sGTM) container. This template will define the logic for capturing e-commerce purchase events and sending the data to Firestore. By creating a reusable template, you streamline the process of handling and managing purchase data across your e-commerce platform.

Write e-commerce Purchases to Firestore with sGTM

You can use this code to accessing write and read data to Firestore from sGTM.

const Firestore = require('Firestore');

const Object = require("Object");

const getTimestampMillis = require("getTimestampMillis");



let writeData = {

timestamp: getTimestampMillis()

};



if (data.customData && data.customData.length) {

for (let i = 0; i < data.customData.length; i += 1) {

const elem = data.customData[i];

if (elem.fieldValue) {

writeData[elem.fieldName] = elem.fieldValue;

} else {

Object.delete(writeData, elem.fieldName);

}

}

}


const rows = writeData;


Firestore.write('<your collection>', rows, {

projectId: '<your google cloud project id>',

merge: true,

}).then((id) => {

data.gtmOnSuccess();

}, data.gtmOnFailure);


Step 2: Configuring the Firestore Database

  1. Open Google Cloud Console: Navigate to the Firestore section.

    Write e-commerce Purchases to Firestore with sGTM


  2. Create Database: Follow the prompts to set up a Firestore database in "production mode" or "test mode" based on your requirements.

Write e-commerce Purchases to Firestore with sGTM


You may also create a new rule like this while you are running test mode: 
Write e-commerce Purchases to Firestore with sGTM

rules_version = '2';



service cloud.firestore {

match /databases/{database}/documents {

match /{document=**} {

allow read, write: if request.auth != null;

}

}

}


Now you can send the any purchase data to Firestore like this: 
Write e-commerce Purchases to Firestore with sGTM

Contact us for more use cases using server-side Google Tag Manager. 

Our Similar Articles in The Product Analytics & Data Category

Privacy Sandbox'a Hazırlık: CHIPS Nedir?
Privacy Sandbox'a Hazırlık: CHIPS Nedir?

Google'ın 2025'teki çerez kaldırma süreciyle, Privacy Sandbox ve CHIPS gibi yeni teknolojiler önem kazanıyor. CHIPS nedir? Hangi tarayıcılarda kullanılır?

Read more
Business Intelligence Reporting: Revolutionizing Digital Marketing Strategies
Business Intelligence Reporting: Revolutionizing Digital Marketing Strategies

Dive into this insightful article to discover how business intelligence reporting can revolutionize your digital marketing strategy, with five transformative ways to harness data-driven insights.

Read more
Importance of Customer Segmentation
Importance of Customer Segmentation

You can easily access a lot of information about customer segmentation, target audience determination, customer analysis by browsing our content.

Read more
We are waiting for you! Contact us now to meet our multidimensional digital marketing solutions.