Skip to content

Commit

Permalink
feat: fix umami page views
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Jun 28, 2024
1 parent 58216b4 commit 824d3fc
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions pkg/tagmanager/server/template/umami.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,12 @@ ___TEMPLATE_PARAMETERS___
___SANDBOXED_JS_FOR_SERVER___
/**
* @description Custom server-side Google Tag Manager Tag Template
* Send events to Umami
* @version 0.1.0
* @see {@link https://github.com/mbaersch|GitHub} for more info
* @see {@link https://umami.is/|Umami Homepage}
*/
const JSON = require('JSON');
const parseUrl = require('parseUrl');
const makeString = require('makeString');
const getAllEventData = require('getAllEventData');
const sendHttpRequest = require('sendHttpRequest');
const getRequestHeader = require('getRequestHeader');
const getContainerVersion = require('getContainerVersion');
const logToConsole = require('logToConsole');
const traceId = getRequestHeader('trace-id');
Expand All @@ -121,9 +112,10 @@ if (pageLocation) {
let umamiEvent = {
type: "event",
payload: {
name: (name === "page_view") ? "" : name,
website: data.websiteId,
name: name,
hostname: hostname,
title: eventData.page_title || "",
url: pageLocation.split(parsedUrl.hostname)[1],
referrer: ref,
language: eventData.language || "",
Expand All @@ -140,9 +132,15 @@ if (pageLocation) {
RequestMethod: 'POST',
RequestUrl: serviceUrl,
RequestBody: umamiEvent,
EventData: eventData,
})
);
const headers = {
'user-agent': eventData.user_agent || getRequestHeader("user-agent"),
'content-type': 'application/json'
};
sendHttpRequest(
serviceUrl, (statusCode, headers, body) => {
logToConsole(
Expand All @@ -163,10 +161,7 @@ if (pageLocation) {
}
},
{
headers: {
'user-agent': eventData.user_agent || getRequestHeader("user-agent"),
'content-type': 'application/json'
},
headers: addRequestHeaders(headers),
method: 'POST',
timeout: data.timeout||1000
},
Expand All @@ -176,6 +171,29 @@ if (pageLocation) {
data.gtmOnFailure();
}
function addRequestHeaders(headers) {
const keys = [
'cf-ipcity',
'cf-ipcountry',
'cf-ipcontinent',
'cf-iplongitude',
'cf-iplatitude',
'cf-region',
'cf-region-code',
'cf-metro-code',
'cf-postal-code',
'cf-timezone',
];
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = getRequestHeader(key);
if (value) {
headers[key] = value;
}
}
return headers;
}
___SERVER_PERMISSIONS___
Expand Down Expand Up @@ -238,7 +256,7 @@ ___SERVER_PERMISSIONS___
},
"param": []
},
"isRequired": true
"isRequired": false
},
{
"instance": {
Expand Down

0 comments on commit 824d3fc

Please sign in to comment.