A
A
alekse314192019-12-04 19:26:17
JavaScript
alekse31419, 2019-12-04 19:26:17

How to get utm tags from url?

You need to get all utm tags from url and pack them into variables.
The result should be:
medium = "x"
source = "y"
campaign = "z"
content = "k"
Url: https://pro-jector.site/?utm_medium=cpc&utm_source...
How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-12-04
@alekse31419

Object.fromEntries((url.match(/(?<=utm_).+?=[^&]*/g) || []).map(n => n.split('=')))

or
[...url.matchAll(/utm_([^=]+)=([^&]*)/g)].reduce((acc, [ , k, v ]) => (acc[k] = v, acc), {})

or
Array
  .from(new URL(url).searchParams)
  .filter(n => n[0].startsWith('utm_'))
  .reduce((acc, n) => ({ ...acc, [n[0].slice(4)]: n[1] }), {})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question