Blog

Use the sGTM logger API for structured debug output

Structured logs are searchable, filterable, and survive past the preview pane.

Custom Templates have a log() function. Most teams use it as console.log. The logger API also accepts structured output.

Basic vs structured

// Basic
log('User data: ' + email);

// Structured
log({
  type: 'user_data_hashed',
  email_present: !!email,
  phone_present: !!phone,
  timestamp: Date.now()
});

Both show in preview. Structured serialises cleanly when forwarded.

Conventions

  • Always include a type field.
  • Include relevant IDs: event_id, transaction_id, user_pseudo_id.
  • Avoid logging PII. Log "email_present: true" not the actual email.
  • Set a log level: log({level: 'warn', ...}).

When to log

On errors and on decisions affecting data quality (filtered events, dedup hits, consent overrides). Sample for ongoing monitoring: if (Math.random() < 0.001) log({...}).

For correlated debugging across the chain, propagate a request_id. The end-to-end flow is much easier to trace with shared IDs.