Server GTM does not have a standard format for output logs. And there is no common way to log Requests and Responses. This creates issues for log parsing and monitoring.
- Each log line must be a valid JSON object. (This allows parse logs by ELK, CloudWatch, Loki, etc.)
- Each log object must contain at least:
Type
,Name
andTraceId
fields. - Field
Type
must contain one of these valuesRequest
,Response
,Message
. Witch helps easily filter logs. TraceId
must contain a unique identifier of the current incoming request. This field is used for stitching all logs done by all tags/clients during one request. Highly recommend using thetrace-id
header for this, as a lot of clouds/platforms use it for tracing.- Field
Name
must contain tag name.
const traceId = loggingEnabled ? getRequestHeader('trace-id') : undefined;
const isLoggingEnabled = determinateIsLoggingEnabled();
const postUrl = 'https://example.com';
const postBody = {'data': 'some data'};
log({
'Name': 'Example',
'Type': 'Request',
'TraceId': traceId,
'EventName': 'purchase',
'RequestMethod': 'POST',
'RequestUrl': postUrl,
'RequestBody': postBody,
});
sendHttpRequest(postUrl, (statusCode, headers, body) => {
log({
'Name': 'Example',
'Type': 'Response',
'TraceId': traceId,
'EventName': makeString(data.conversionActionId),
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
});
}, {method: 'POST'}, JSON.stringify(postBody));
function log(logObject) {
if (isLoggingEnabled) {
logToConsole(JSON.stringify(logObject));
}
}
function determinateIsLoggingEnabled() {
const containerVersion = getContainerVersion();
const isDebug = !!(
containerVersion &&
(containerVersion.debugMode || containerVersion.previewMode)
);
if (!data.logType) {
return isDebug;
}
if (data.logType === 'no') {
return false;
}
if (data.logType === 'debug') {
return isDebug;
}
return data.logType === 'always';
}
{
"displayName": "Logs Settings",
"name": "logsGroup",
"groupStyle": "ZIPPY_CLOSED",
"type": "GROUP",
"subParams": [
{
"type": "RADIO",
"name": "logType",
"radioItems": [
{
"value": "no",
"displayValue": "Do not log"
},
{
"value": "debug",
"displayValue": "Log to console during debug and preview"
},
{
"value": "always",
"displayValue": "Always log to console"
}
],
"simpleValueType": true,
"defaultValue": "debug"
}
]
}
@adatzer @paulboocock @jurajfrank