Stape/Documentation

Stape Node.js SDK

Updated Mar 24, 2025

Overview of Stape Node.js SDK

With Stape sGTM NodeJS SDK you can implement server-side Google Tag Manager into the Note.js applications. It includes easy settings adjustments, the ability to send specific event data, and the option to modify this data through actions like trimming and encoding.

Also, you can create an event and send it to the Data Client. SDK API provides a response from the Data Client mapped to a simple model that contains serialized response fields.

How to set up Stape Node.js SDK

Configuration

Fill in the basic parameters:

[object HTMLPreElement]
VariableDescription
gtm_server_domainServer host
request_pathRequest processing path

Sending Event Data

[object HTMLPreElement]
VariableDescription
eventNameEvent name
eventDataArray of options for forming event data

eventData

[object HTMLPreElement]
OptionDescription
nameVariable name
valueVariable value

Transformations

OptionDescription
trimRemoves whitespace from the beginning and end of the value
base64Encodes the string in Base64 format
md5Encodes the string in MD5 format
sha256base64Encodes the string in SHA256 Base64 format
sha256hexEncodes the string in SHA256 HEX format

Full Example

[object HTMLPreElement]

Stape Data Client in server GTM

1. Download Data Client from GitHub.

2. Go to your server GTM container ➝ Templates ➝ Add new Client template and select .tpl file that you loaded from GitHub.

3. Go to ‘Clients’ section on your server GTM container ➝ New client ➝ Add Data Client from templates.

4. Submit changes on your server GTM container to make it live.

How to add Stape Node.js SDK to your project

1. Install the code. 

npm install stape-sgtm-nodejs

2. Initialize the SDK in your Node.js project.

import StapeSGTM, { transformations, EventData } from 'stape-sgtm-nodejs'; const sgtm = new StapeSGTM({   gtm_server_domain: 'https://gtm.example.com',   request_path: '/data', }); const eventData = {   value: 68,   currency: 'USD',   page_hostname: 'stape.io',   page_location: 'http://stape.io',   user_data: {     email: transformations.sha256hex('jhonn@doe.com'),   }, }; sgtm.sendEventData('add_to_cart', eventData)   .then((result) => console.log('✅ Event Sent:', result))   .catch((error) => console.log('❌ Error:', error));

How to test the setup

The server GTM preview only displays incoming requests sent from the tab adjacent to the Chrome browser where the preview mode runs. So, if you run sGTM preview and send a request through SDK, you will not see it in preview mode, although this does not prevent it from working.

You need to use the power-up sGTM Preview Header config to view and debug SDK requests. We have a blog post describing setting up and debugging incoming webhooks in the sGTM.

If you are configuring this on a live container that receives a lot of hits, activating the HTTP header may not be a good idea. It will display all requests coming to the server in the preview, which can lead to performance issues for the container if the preview receives hundreds or thousands of requests per second. 

In such a case, it would be a good idea to start a ‘staging’ container on a free plan, where everything is configured and debugged, and then transfer that to a live container and the live URL of your server GTM.

The most common problem when using the SDK is getting a 400 error

400 error means that no client has processed your request. In this case, make sure that Data Client is installed on your server GTM container, and that these changes are omitted (check in the Versions tab that the latest version of the container is live).

Also make sure that the /path to which you send requests is specified in the Data Client (if you use something other than the standard /data).

Can’t find what you are looking for?