Get referrer for the current URL being requested and append it as a UTM parameter

If someone clicks from our website e.g. to our product i.e. - we wish to automatically add UTM parameters to track the source of the signup within the URL, via the referrer value in the request. Ideally, the URL then served to the user would be something like this:

In this case, utm_term would be the portion of the URL in the referrer from the root domain i.e. in this case would be where they clicked before they arrived at the signup page.

How would I implement this automation of utm_tag additions via a Cloudflare worker? Any sample code?

1 Like