我想从URL中提取3个utm (utm_source, utm_medium, utm_campaign),并将它们发送到zapier中的这个javascript代码步骤的下一步。
javascript代码运行完美:
let url = inputData.URL;
let queryString = url.split("?").pop();
let urlParams = new URLSearchParams(queryString);
let utm_source = urlParams.get('utm_source')
let utm_campaign = urlParams.get('utm_campaign')
let utm_medium = urlParams.get('utm_medium')
let utm_term = urlParams.get('utm_term')
let utm_content = urlParams.get('utm_content')
output = [{url, queryString, utm_source, utm_campaign, utm_medium, utm_term, utm_content}];
当URL没有UTM时,问题就出现了。在本例中,该值为空,因此我想用默认值(例如"organic")
替换它。我开始尝试这样做,但它不工作:
if(utm_campaign === null || typeof utm_campaign === "undefined" || utm_campaign === "") {
utm_campaign = "ORGANIC";
}
请,谁能帮我这个javascript代码?提前谢谢你
这是使用ternary
运算符的绝佳机会:
let url = inputData.URL;
let queryString = url.split("?").pop();
let urlParams = new URLSearchParams(queryString);
let utm_source = urlParams.get('utm_source') ? urlParams.get('utm_source') : 'definitely not organic'
let utm_campaign = urlParams.get('utm_campaign')
let utm_medium = urlParams.get('utm_medium') ? urlParams.get('utm_medium') : 'organic'
let utm_term = urlParams.get('utm_term')
let utm_content = urlParams.get('utm_content')
output = [{url, queryString, utm_source, utm_campaign, utm_medium, utm_term, utm_content}];
在这种情况下,您可以选择默认的per变量。