0

I will have a variety of URLs that all contain the same Query Parameter:

https://www.example.com/landing-page/?aid=1234

I would like to extract the "1234" by searching for the "aid" query parameter in the URL.

The javascript will run in Zapier:

Example javascript block in Zapier

Zapier notes: What input data should we provide to your code (as strings) via an object set to a variable named inputData?

I don't have much experience with javascript or coding in general, but the end result would be the 4-digit "aid" value that I would then reference when posting via webhook to an API.

edit: I checked the similar answers and appreciate the links however I am not sure how to utilize "inputData" and "url" in Zapier with the provided answers.

  • Start here: https://regex101.com/ –  Aug 01 '18 at 19:33
  • 2
    https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript - Flagging as duplicate - excellent answer there with 7k votes!!! – JasonB Aug 01 '18 at 19:33
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – JasonB Aug 01 '18 at 19:34

1 Answers1

1

David here, from the Zapier Platform team.

While the comments above point you towards regex, I recommend a more native approach: actually parsing the url. Node.js has a great standard library for doing that:

// the following line is set up in the zapier UI; uncomment if you want to test locally
// const inputData = {url: 'https://www.example.com/landing-page/?aid=1234'}

const url = require('url')
const querystring = require('querystring')

const urlObj = url.parse(inputData.url) /*
Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.example.com',
  port: null,
  hostname: 'www.example.com',
  hash: null,
  search: '?aid=1234',
  query: 'aid=1234',
  pathname: '/landing-page/',
  path: '/landing-page/?aid=1234',
  href: 'https://www.example.com/landing-page/?aid=1234' }
*/
const qsObj = querystring.parse(urlObj.query) // { aid: '1234' }

return { aid: qsObj.aid }

Depending on how confident you are that the data you're looking for will always be there, you may have to do some fallbacks here, but this will very reliably find the param(s) you're looking for. You could also follow this code step with a Filter to ensure latter steps that depend on the aid don't run if it's missing.

​Let me know if you've got any other questions!

xavdid
  • 5,092
  • 3
  • 20
  • 32
  • Awesome thanks so much for helping out! I ended up using the following code but will replace with yours: `var url = inputData.url; function getParameterByName(name, url) { name = name.replace(/[\[\]]/g, '\\$&'); var regex = new RegExp('[?&]' + name + '(=([^]*)|&|#|$|%)'), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, ' ')); } var aid = getParameterByName('aid',url); return {aidval: aid, url: inputData.url};` – flyeaglesfly Aug 02 '18 at 22:36
  • Great! If this answer solved your problem, make sure to upvote it and mark it as correct ([info](https://stackoverflow.com/help/someone-answers)). – xavdid Aug 03 '18 at 03:23
  • Is there a way to populate the `aid` with a default 4 digit code if there is no `aid` query in the URL? It seems the `aid` is a required field for posting the API regardless of if it exists in the URL or not, so I have been provided a defaulted `aid` to use in case it doesn't exist in URL. I'm wondering how I might be able to edit your code block to include that logic. – flyeaglesfly Aug 08 '18 at 18:33
  • Yep, you can modify the last line to be `return { aid: qsObj.aid || 'DEFAULT CODE' }` – xavdid Aug 08 '18 at 19:30