AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Data Studio
3.7K members online now
3.7K members online now
Ask questions about Data Studio
Guide Me
star_border
Reply

Substring of URL as Custom Dimension

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi Guys!

 

My site's urls have the following structure:

 

www.mysite.com/page/sub_page?source_information

 

I'd like to do some analytics by looking at each page individually and ignoring the source_information part of the url. My hope was to truncate all urls before the '?'. Does anyone know how I would construct this custom dimension formula? 

Substring of URL as Custom Dimension

Explorer ✭ ✭ ✭
# 2
Explorer ✭ ✭ ✭

Hi Kyle,

 

I would do this by creating a custom metric using REGEX_EXTRACT to pull out the string after the first "/" and before the "?".

 

Here is some more information on regex_extract:

https://support.google.com/datastudio/answer/7050487?hl=en

 

Hope that helps,

Ben

Substring of URL as Custom Dimension

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Hi Ben, 

 

Thanks for the response! I've tried the following formula but it keeps returning an error:

 

CASE WHEN REGEXP_MATCH(Landing Page, '.*?.*') THEN REGEXP_EXTRACT(Landing Page, '^[^\?]*') ELSE Landing Page

 

My hunch is that there's something wrong with the second regular expression but I'm new to the syntax, so it could be something else. Any thoughts?

Substring of URL as Custom Dimension

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

Sorry, this is what I had:

 

CASE WHEN REGEXP_MATCH(Landing Page, '.*\?.*') THEN REGEXP_EXTRACT(Landing Page, '^[^\?]*') ELSE Landing Page  END

Substring of URL as Custom Dimension

Explorer ✭ ✭ ✭
# 5
Explorer ✭ ✭ ✭

Hi Kyle,

 

I would try:

REGEXP_EXTRACT(Landing Page, '\/(.*?)\?')

 

 

As a simple grab of whats between the first / and the ?.

 

Hope that helps,

Ben

Substring of URL as Custom Dimension

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭

Ok. I tried that with and without the case statement and am still getting an error. Do you need a case statement for this type of formula?

Substring of URL as Custom Dimension

Explorer ✭ ✭ ✭
# 7
Explorer ✭ ✭ ✭

Hi Kyle,

 

No you don't, just the formula above should work. If it's not working for you it could be that some of the regex used is not supported in DS, try swapping it for something similar, as a test you could try \/(.*) to grab everything after the /.

 

Ben

Substring of URL as Custom Dimension

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭

Weird. I tried that too and it's returning an error. I did just try the following and it accepted the formula but the only dimension displayed is null. 

 

REGEXP_EXTRACT(Landing Page, '^[^\?]*')

 

Pretty sure the regular expression in the above formula is the one I want, it's just not returning any data. Sorry to bother you, you've been very helpful

Substring of URL as Custom Dimension

Explorer ✭ ✭ ✭
# 9
Explorer ✭ ✭ ✭

No problem! Your regex works when I test it out on http://regexr.com, so it must be a support issue with DS, assume your field name is definitely correct. I appreciate that's not very helpful.

 

One other thing you could try is REGEX_REPLACE and replace the parts of the URL you don't want with a blank space to strip them out. Though I would expect you'd get the same errors, it's worth a shot!