2

I want to track subfolders in Google Analytics, which are on one to five levels.

Additional obstacle is, that subfolders don't own in 100% cases trailing slash and i need to track both kind of them, with and without trailing slash, based only on the subfolder level, also /a-1/ and /a-1, /a-1/b-2/ and /a-1/b-2 and so on.

Folders look like /a-1/ and /a-1, /a-1/b-2/ and /a-1/b-2, /a-1/b-2/c-3/ and /a-1/b-2/c-3, /a-1/b-2/c-3/d-4/ and /a-1/b-2/c-3/d-4, /a-1/b-2/c-3/d-4/e-5/ and /a-1/b-2/c-3/d-4/e-5, and i need to track both of /a-1/ and /a-1 etc.

My idea was to count slashes to get the level of subfolder and then to make clear whether there is a traling slash or not to get bot variants. According to this idea matching of two subfolders, /a/b/ and /a/b would contain two rules:

  • if there are 3 slashes and the string ends up with slash
  • if there are 2 slashes and the string ends up without slash

This one, (.*\/){3}\/$, should match strings, which have 3 slashes and end up with slash,

Another one, (.*\/){2}[^\/]$, should match strings, which have 2 slashes and end up without slash.

Both rules don't work, not alone, not joined together with the pipe, as (.*\/){3}\/$|(.*\/){2}[^\/]$ - they just match something not expected.

Point me please to the solution, to the right direction.

1 Answer 1

3

It becomes a real rule for me: barely i become exasperated with finding of a problem's solution and post a question at StackOverflow, directly after posting i find an answer by myself :)

Well, the regular expressions, which do the job together are two: first to define the nesting level, and the second to exclude nesting levels higher as the defined.

For example, we want to create a filter matching subfolders on second nesting level with and without trailing slash. They look like /a-1/b-2/ and /a1/b-2

  1. Matching needed subfolders: (/[^\s]+){2}?$
  2. Excluding subfolders from higher nesting level: (/[^\s]+){3}?$

To make subfolder statistics always and easy available, we create shortcuts for each filter combination.

Done - enjoy!

Comments

Enter at least 15 characters
Enter at least 15 characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.