DEV Community

Marcin Masłowski
Marcin Masłowski

Posted on

How to get query parameter from URL in Google Data Studio

For example, you want to get query parameters from URLs like this:
www.mystore.com/search?q=shoes&color=red

To simplify the process I use Google Sheets as a data source. But you can use raw data from Google Analytics as well:

Alt Text

When we have this data in Google Data Studio our goal is to have query parameter as separate columns in our report like this:

Alt Text

To do this we have to create, a new field, and use REGEXP to extract the data from URLs:

Alt Text

In code below "q" is the query parameter you want to extract for example "color":

REGEXP_EXTRACT(URL, 'q=([^&]+)')
~~~~

If you use Google Analytics as a data source, change the URL with PAGE dimension:
Enter fullscreen mode Exit fullscreen mode

REGEXP_EXTRACT(Page, 'q=([^&]+)')
~~~~

Now we can create reports based on new fields:

Alt Text

Top comments (1)

Collapse
 
adamthedev profile image
Adam Jones

This looks very close to what I need.
We have a search page that may have one or more of a collection of about 12 url params added to it, depending on optional settings a user may select when they run a search.

I'm setting these up as follows

REGEXP_EXTRACT(Page, 'swimmingpool=Yes')

Does that look right? And then I'd add them as a "metric" to my report ?