Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 6 min read

A Tiny UTM Link Builder in Plain JavaScript for Filipino Shops Selling on Facebook and TikTok

By MD Fozla Rabbi, SEO Specialist A friend runs a small milk tea shop in Quezon City. She sells on Facebook, TikTok and a little on Instagram. Orders come through a simple order page on her site. Last month she asked m

By MD Fozla Rabbi, SEO Specialist

A friend runs a small milk tea shop in Quezon City. She sells on Facebook, TikTok and a little on Instagram. Orders come through a simple order page on her site.

Last month she asked me a fair question. "Which app actually brings the orders?"

I opened her analytics. Almost everything said direct or (not set). No idea where people came from. Her links in bios and captions were just bare URLs.

The fix is boring and old: UTM parameters. Tiny tags at the end of a link that tell your analytics where a click came from. The problem is nobody types them right by hand. One post says facebook, the next says FB, the next says Facebook with a trailing space. Now you've got three sources that are really one.

So I wrote her a tiny UTM link builder in plain JavaScript. One HTML file. Works offline. Keeps the naming consistent. Here's how it works.

What a UTM link looks like

https://example.ph/order?utm_source=tiktok&utm_medium=social&utm_campaign=nov-payday-promo&utm_content=bio-link

Four tags do most of the work:

  • utm_source: the app or site, like facebook or tiktok
  • utm_medium: the type of traffic, like social or paid-social
  • utm_campaign: the promo or theme, like nov-payday-promo
  • utm_content: the exact spot, like bio-link or reel-caption

Google Analytics reads these automatically. You don't need any setup beyond having analytics on the page.

Step 1: lock down the allowed values

The biggest win isn't the code. It's deciding names once. So we hard-code the sources and mediums the shop actually uses.

const SOURCES = ["facebook", "tiktok", "instagram", "messenger", "viber", "shopee-chat"];
const MEDIUMS = ["social", "paid-social", "message", "bio"];

Viber and Messenger are on the list for a reason. A lot of Filipino customers share order links in group chats. Those clicks deserve their own label instead of getting lumped into "direct".

Step 2: clean whatever the user types

Campaign names come from humans. Humans type "Nov Payday PROMO!!". We want nov-payday-promo.

function slugify(text) {
  return text
    .toString()
    .normalize("NFKD")
    .replace(/[\u0300-\u036f]/g, "")
    .toLowerCase()
    .trim()
    .replace(/[^a-z0-9]+/g, "-")
    .replace(/^-+|-+$/g, "")
    .slice(0, 60);
}

slugify("Nov Payday PROMO!!");   // "nov-payday-promo"
slugify("Pasko Sale – Γ‘ edition"); // "pasko-sale-n-edition"

The normalize("NFKD") line splits characters like Γ± into n plus an accent mark. Then the next line removes the accent. Handy for Filipino words with Γ±.

Step 3: build the link safely

Don't glue strings together with + and &. The URL API handles encoding and existing query strings for you.

function buildUtmLink({ baseUrl, source, medium, campaign, content }) {
  let url;
  try {
    url = new URL(baseUrl.trim());
  } catch {
    throw new Error("That doesn't look like a full URL. Include https://");
  }
  if (!SOURCES.includes(source)) throw new Error(`Unknown source: ${source}`);
  if (!MEDIUMS.includes(medium)) throw new Error(`Unknown medium: ${medium}`);

  const camp = slugify(campaign);
  if (!camp) throw new Error("Campaign name is required");

  url.searchParams.set("utm_source", source);
  url.searchParams.set("utm_medium", medium);
  url.searchParams.set("utm_campaign", camp);
  if (content) url.searchParams.set("utm_content", slugify(content));
  return url.toString();
}

If the base URL already has ?ref=abc, searchParams.set keeps it and adds the UTM tags after it. If someone pastes a link that already has old UTM tags, set overwrites them instead of duplicating. Both mistakes happened in the first week. Before the fix, obviously.

Step 4: a tiny form around it

Here's the whole page. Save it as utm.html and open it in any browser. No server needed.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>UTM Builder</title>
<style>
  body { font-family: system-ui, sans-serif; max-width: 480px; margin: 2rem auto; padding: 0 1rem; }
  label { display: block; margin-top: .8rem; font-weight: 600; }
  input, select, button { width: 100%; padding: .5rem; font-size: 1rem; }
  #out { word-break: break-all; background: #f3f3f3; padding: .7rem; margin-top: 1rem; }
</style>
<h1>UTM link builder</h1>
<label>Page URL <input id="base" placeholder="https://yourshop.ph/order"></label>
<label>Source <select id="source"></select></label>
<label>Medium <select id="medium"></select></label>
<label>Campaign <input id="campaign" placeholder="Nov payday promo"></label>
<label>Content (optional) <input id="content" placeholder="bio link"></label>
<button id="go" style="margin-top:1rem">Build link</button>
<div id="out"></div>
<script>
  /* paste SOURCES, MEDIUMS, slugify and buildUtmLink here */
  const fill = (id, list) => document.getElementById(id).innerHTML =
    list.map(v => `<option>${v}</option>`).join("");
  fill("source", SOURCES);
  fill("medium", MEDIUMS);

  document.getElementById("go").onclick = async () => {
    const out = document.getElementById("out");
    try {
      const link = buildUtmLink({
        baseUrl: base.value, source: source.value, medium: medium.value,
        campaign: campaign.value, content: content.value
      });
      out.textContent = link;
      await navigator.clipboard?.writeText(link);
      out.textContent += "  (copied)";
    } catch (e) {
      out.textContent = e.message;
    }
  };
</script>
</html>

The clipboard part only works on https or a local file in most browsers. If it fails, the link still shows on screen. Good enough.

Step 5: keep a log of every link you made

Two weeks in, she asked, "What did I call the Undas promo again?" Fair. So the page also saves each link in localStorage.

function saveLink(link) {
  try {
    const log = JSON.parse(localStorage.getItem("utmLog") || "[]");
    log.unshift({ link, at: new Date().toISOString() });
    localStorage.setItem("utmLog", JSON.stringify(log.slice(0, 200)));
  } catch {
    /* storage blocked, skip quietly */
  }
}

Call saveLink(link) right after building it. Add a small list under the form if you want. I kept it simple.

What changed after a month

Her analytics finally had names instead of "(not set)". A few surprises:

TikTok brought more visits but Facebook brought more orders. TikTok visitors browsed and left. Facebook visitors, many from her page's pinned post, actually checked out.

Messenger and Viber shares were bigger than expected. Almost a fifth of orders came from links people forwarded in chats. She'd never have known.

Bio links beat caption links. On TikTok the bio-link content tag outperformed caption links by a wide margin.

So she moved effort, not budget. More Facebook posts with order links. A clear "share this with your barkada" line in captions. A cleaner TikTok bio.

That's the real point of tracking. Social media works differently for every business type and the data tells you which part works for yours. If you're explaining to a client why this matters at all, this piece on the importance of social media for business growth in the Philippines lays out the bigger picture with local usage numbers.

A few naming rules that saved us

  • Always lowercase. Analytics treats Facebook and facebook as different sources.
  • Use hyphens, never spaces.
  • Put the month in the campaign name, like dec-pasko-sale. Future you will thank you.
  • Don't tag internal links on your own site. UTM tags are for links coming in from outside.

Wrapping up

This whole tool is under 100 lines. No framework. No build step. It just stops people from typing tracking links by hand.

I've shared versions of it with a few other small Philippine sellers too. If you work with local shops or run a page yourself, the folks at BoostingPH write a lot about which platforms actually move Filipino audiences, which pairs nicely with having clean data of your own.

Got a better naming scheme? Or a platform I missed, like Lazada chat? Tell me in the comments. I'll add it to the list.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.