A couple of years ago, I began using a Cloudflare worker to track my Googlebot traffic in Google Analytics. The initial code I used, and thought process to get there, is here, but here's what my code looks like today:
const analyticsId = 'UA-98289-3'
addEventListener('fetch', event => {
event.passThroughOnException()
event.respondWith(handleRequest(event))
})
/**
* Check request object for Googlebot UA to send tracking data
* @param {Event} event
*/
async function handleRequest(event) {
let request = event.request
let userAgent = request.headers.get('user-agent')
let response = await fetch(request)
if (userAgent)
{
// If Googlebot, then track hit in Analytics
if (userAgent.includes('Google')) {
// Search html
let clone = await response.clone()
let html = await clone.text()
let indexableStatus = html.includes('<meta name="robots" content="noindex">') ? 'noindex' : 'indexable'
event.waitUntil(analyticsHit(
{
// uip: request.headers.get('CF-Connecting-IP'),
dl: request.url,
dt:
response.status + ' ' +
response.statusText + ' ' +
indexableStatus + ' ' +
userAgent
}
))
}
}
// Return the original content
return response
}
/**
* Send bot tracking data using Analytics Measurement Protocol
* @param {Object} tracking
*/
function analyticsHit(tracking) {
let payload = 'v=1&t=pageview&tid=' + analyticsId
for(var key in tracking) {
payload += '&' + key + '=' + encodeURIComponent(tracking[key])
}
payload += '&cid=' + [ Math.round(Math.random() * 2147483647), Math.round(new Date() / 1000.0) ].join('.')
return fetch('https://www.google-analytics.com/collect', {
method: 'POST',
body: payload
})
}
Note this is in a Cloudflare worker, so it executes at the edge. Now with Google Analytics being retired, and GA4 not supporting API usage in this way, I'm looking to freshen up this code a bit. However, I'm a javascript n00b. I pieced this code together from bits and snippets of using await async that I found on the web, and now I'm realizing that I'm not 100% sure that I'm doing it right. It's my goal to not delay my website's page load time while it sends off an API code to Google tracking. And yet I have event.waitUntil(analyticsHit()
... Is it correct that I am using waitUntil here? Is that telling Cloudflare to not load the page until a response has been received?