Set default values for missing URL parameters in Zapier JavaScript

I’m working on a Zapier automation where I need to parse marketing parameters from URLs using JavaScript. My code extracts tracking parameters from URLs but I’m having trouble setting fallback values when these parameters don’t exist.

Here’s my current working code:

let targetUrl = inputData.URL;
let paramString = targetUrl.split("?").slice(-1)[0];
let params = new URLSearchParams(paramString);
let source = params.get('utm_source');
let campaign = params.get('utm_campaign');
let medium = params.get('utm_medium');
let term = params.get('utm_term');
let content = params.get('utm_content');

output = [{targetUrl, paramString, source, campaign, medium, term, content}];

The issue is when URLs don’t have these tracking parameters. The values come back empty and I want to set them to “organic” as a default.

I tried this approach but it doesn’t work:

if(campaign === null || typeof campaign === "undefined" || campaign === "") {
  campaign = "ORGANIC";
}

Can anyone help me figure out the right way to handle these empty values in JavaScript? Thanks for any help.

I’ve hit this same URL parsing problem in Zapier workflows. Your URL splitting logic is probably creating edge cases. When there’s no query string, split("?").slice(-1)[0] returns the entire URL instead of an empty string, which breaks URLSearchParams. Here’s what’s worked reliably for me: javascript let targetUrl = inputData.URL; let url = new URL(targetUrl); let source = url.searchParams.get('utm_source') || 'organic'; let campaign = url.searchParams.get('utm_campaign') || 'organic'; let medium = url.searchParams.get('utm_medium') || 'organic'; let term = url.searchParams.get('utm_term') || 'organic'; let content = url.searchParams.get('utm_content') || 'organic'; The URL constructor handles malformed URLs better, and searchParams is way more reliable than manual parsing. The || operator catches null, undefined, and empty strings all at once. This approach has been rock solid across different URL formats in my production zaps.

use the logical or operator - way cleaner. just do let campaign = params.get('utm_campaign') || 'organic'; for each param. if params.get returns null, it’ll automatically fall back to your default. works perfectly in zapier js.

Your conditional logic looks right, but there’s an easier way. Try the nullish coalescing operator: campaign = params.get('utm_campaign') ?? 'organic'. It handles null and undefined while keeping empty strings if you need them. URLSearchParams.get() usually returns null for missing parameters, so your if statement should work. Add a console.log before it to make sure you’re hitting that condition. Sometimes the parameter exists but it’s empty, which means it’s not null. For something more bulletproof, use a helper function that catches all falsy values: function setDefault(value, defaultVal) { return value || defaultVal; } then call it like campaign = setDefault(params.get('utm_campaign'), 'organic').