I used Google Data Studio to combine a large website sitemap containing the Page URL with the Google Analytics performance data for the same Page URLs going back five years. I am attempting now to identify the navigational depth of each of those URLs based on their length and the number of forward slashes in the URL (/
). Navigational depth being 0 for the home page and 1 for pages beneath it, etc.
I have a segment of code I've been working on to do this:
LENGTH(REGEXP_REPLACE(Page, "[^\\/]/g", "")) - 1
Briefly, if I understand the code correctly I've put together, I am seeking to identify all characters in the Page
value that are not forward slashes (/
) and using REGEXP_REPLACE
, substitute them with
(nothing), leaving essentially only the forward slashes remaining and then counting their length to determine how many there are and thus the navigational depth minus 1 to remove the first in all Page URLs. /g
is a global variable that I've included because it doesn't work otherwise in an REGEX creators I've been using to test the code. The double escaped \\
is a particularity of Google Data Studio as best as I can tell.
The field in question contains data that looks like this:
/testing-data/page-123/page-321/name-of-page/
My code should result in the result 4
(5-1) but is currently resulting in 1
.