Using Stape SDKs for server-side tracking

Written by

Alex Held

Updated Apr 8, 2025 comments

What is SDK?

Getting started with Stape SDK

How to set up Stape Data Client in server GTM

How to add Stape SDK to your project

Node.js SDK

npm install stape-sgtm-nodejs
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));

PHP SDK

composer require stape/stape-sgtm-php
require_once __DIR__ . '/vendor/autoload.php'; use Stape\Sgtm\StapeSGTM; use Stape\Sgtm\Transforms; $sgtm = StapeSGTM::create('https://gtm.example.com', '/data'); $eventData = [     'value' => 68,     'currency' => 'USD',     'page_hostname' => 'stape.io',     'page_location' => 'http://stape.io',     'user_data' => [         'email' => Transforms::sha256hex('jhonn@doe.com'),     ], ]; $response = $sgtm->sendEventData('add_to_cart', $eventData); if ($response) {     echo "✅ Event sent successfully!"; } else {     echo "❌ Failed to send event."; }

iOS SDK

pod 'StapeSDK'
pod install
import StapeSDK @main class AppDelegate: UIResponder, UIApplicationDelegate {     var window: UIWindow?     func application(_ application: UIApplication,                      didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {         // Initialize Stape SDK with your domain         if let url = URL(string: "https://gtm.example.com") {             let configuration = Stape.Configuration(domain: url)             Stape.start(configuration: configuration)         }         return true     } }
import StapeSDK class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Send "add_to_cart" event sendAddToCartEvent() } func sendAddToCartEvent() { // Define event payload let payload: [String: Any] = [ "value": 68, "currency": "USD", "page_hostname": "stape.io", "page_location": "http://stape.io", "user_data": [ "email": sha256("jhonn@doe.com") // Hash email for privacy ] ] // Create event let event = Stape.Event(name: "add_to_cart", payload: payload) // Send event Stape.send(event: event) { result in switch result { case .success(let response): print("✅ Event Sent: \(response.payload)") case .failure(let error): print("❌ Error: \(error.localizedDescription)") } } } // SHA256 hashing function func sha256(_ input: String) -> String { guard let data = input.data(using: .utf8) else { return "" } var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH)) data.withUnsafeBytes { _ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash) } return hash.map { String(format: "%02x", $0) }.joined() } }

Firebase hooking

public static func addFBEventHandler(_ handler: @escaping Completion, forKey key: String) public static func removeFBEventHandler(forKey key: String)

Android SDK

implementation 'io.stape:android-sgtm:1.0'
import android.app.Application; import io.stape.sgtm.Options; import io.stape.sgtm.Stape; public class MyApp extends Application {     @Override     public void onCreate() {         super.onCreate();         Options options = new Options("gtm.example.com");         Stape stape = Stape.withOptions(options);     } }
import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import io.stape.sgtm.Stape; import java.util.HashMap; import java.util.Map; public class MainActivity extends AppCompatActivity {     private Stape stape;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         // Initialize Stape instance         stape = Stape.getInstance();         // Send "Add to Cart" event         sendAddToCartEvent();     }     private void sendAddToCartEvent() {         // Create event data         Map<String, Object> eventData = new HashMap<>();         eventData.put("value", 68);         eventData.put("currency", "USD");         eventData.put("page_hostname", "stape.io");         eventData.put("page_location", "http://stape.io");         eventData.put("user_data", getUserData());         // Send event         stape.sendEvent("add_to_cart", eventData);     }     private Map<String, Object> getUserData() {         Map<String, Object> userData = new HashMap<>();         userData.put("email", sha256("jhonn@doe.com"));         return userData;     }     private String sha256(String input) {         try {             MessageDigest digest = MessageDigest.getInstance("SHA-256");             byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));             StringBuilder hexString = new StringBuilder();             for (byte b : hash) {                 String hex = Integer.toHexString(0xff & b);                 if (hex.length() == 1) hexString.append('0');                 hexString.append(hex);             }             return hexString.toString();         } catch (NoSuchAlgorithmException e) {             throw new RuntimeException(e);         }     } }

Example of using

import StapeSGTM, { transformations, EventData } from 'stape-sgtm-nodejs'; const sgtm = new StapeSGTM({   gtm_server_domain: 'https://gtm.example.com', //your sgtm container url   request_path: '/data', //default Data Client path }); const eventData = {   value: 50.69,   currency: 'USD',   items: [     {       item_id: '42082',       item_name: 'LEGO Technic 42082',       quantity: 1,       price: 50.69,     }   ],   content_ids: ["42082"],   page_hostname: 'lego.com',   page_location: 'http://lego.com/products/lego-technic-42082',   page_path: '/products/lego-technic-42082',   page_title: 'LEGO Technic 42082',   page_referrer: 'http://google.com',   ip_override: '195.234.12.69',   user_agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120',   language: 'en',   user_data: {     email: 'jhonn@doe.com',     first_name: 'Jhonn',     last_name: 'Doe',     user_id: '437283'   },   facebook_click_id: 'IwY2xjawIgS11leHRuA2FlbQEwAGFkaWQBqxhD-YazOQEdX6Vv2-TU2AdiARCxJoWJWZBwf1IDdMusnodh0C47KbqQta4iab5tLmqj_aem_2BKiIsr7_LBVloJPeSYSLw', }; sgtm.sendEventData('purchase', eventData)   .then((result) => console.log('✅ Event Sent:', result))   .catch((error) => console.log('❌ Error:', error));

Testing the setup

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

Final thoughts

Want to start using server-side tags?

Stape has lots of tags for server GTM! Click on Try for free to register and check them all.

Try for free

Written by

Alex Held

Content writer

Alex, a tracking expert, specializes in first-party data, enrichment, and measurement. With GTM and sGTM expertise, he helps businesses enhance data accuracy, attribution, and audience insights.

Was this article helpful?

Comments

Recommended for you

Turn better tracking into better marketing performance

Try for free