I've been writing a small script to read UTM parameters from incoming URLs and clean them before they're stored. It looks simple, but the edge cases add up quickly:
Case and spacing: "Facebook", "facebook", and "facebook " should map to one value
URL encoding: %20, +, and double-encoded values
Duplicates: the same parameter appearing twice in one query string
Missing values: utm_source= with nothing after it
Lost parameters: redirects that strip the query string before it reaches your code
For reporting, I normalize everything to lowercase and keep a list of allowed source and medium values. A tracker like
UTMCAP handles the click and conversion side, so my script only has to produce clean tags.
How do you handle query string parsing in your own projects? Do you write it by hand or use a library?