Search code examples
analyticsadobe-analytics

URL Type Hierarchy in Adobe DTM


I'm trying to create a page type hierarchy where I can use it both a page hierarchy as well as props and evars, using the page URL. In a nutshell my URL would look something like this:

http://www.domain.com/BrandHomePage/SuperCategory/ProductCategory/Product

The mindset is to take the URL and use a data element to split the URL, and then capture the values into separate data elements that could also be used in a page hierarchy.

var url = "http://www.domain.com/part1/part2/part3/part4"
var parts = url.split('/').splice(2);
console.log(parts);

var baseUrl = parts[0];
var part1 = parts[1];
var part2 = parts[2];
var part3 = parts[3];
var part4 = parts[4]

My question is, would it even be possible to capture each individual portion of the URL into separate data elements? Or is my approach overkill.


Solution

  • Create a Data Element

    The following will create a Data Element that returns an array containing up to 4 elements, depending on how many dir levels there are in the URL.

    Go to Rules > Data Elements > Create New Data Element

    Name it "hier1" (no quotes).

    Choose Type Custom Script and click Open Editor.

    Add the following code to the code box:

    return location.pathname.split('/').filter(Boolean).slice(0,4);

    When you are done, Save Changes.

    Populate the Hierarchy Variable

    Here is an example of populating hier1 on page view.

    Go to Overview > Adobe Analytics Tool Config > Pageviews & Content

    Under Hierarchy, select Hierarchy1 from the dropdown (this is shown by default).

    To the right of the dropdown, in the first field, add %hier1%

    Leave the other 3 fields blank.

    Leave Delimiter as default comma , (it doesn't matter what you put here).

    Note: DTM stringifies the returned array (String(Array) or Array.toString()) from the Data Element, which is effectively the same as doing Array.join(','). This is why the above shows to only put the Data Element reference in the first field, and the Delimiter is ignored.

    If your implementation uses a delimiter other than a comma, see additional notes below.

    Additional Notes

    Populating other Variables

    You can also reference %hier1% to populate other variable fields in the Global Variables section. Note that the data element will be stringified with default comma delimiter.

    Alternatively, you may consider using Dynamic Variable syntax (e.g. D=h1) as the value, to shorten the request URL. If you are using the latest AppMeasurement and Marketing Cloud Service libraries, this isn't a big deal (the libs will automatically use a POST request instead of GET request if the request URL is too long).

    Using the Data Element in Custom Code Boxes

    You can use _satellite.getVar('hier1') to return the data element. Note that this returns an array, e.g. ['foo','bar'], so you need to use .join() to concatenate to a single delimited string value.

    Using a different Delimiter

    If your implementation uses a delimiter other than a comma (,) and you use the same alternate delimiter for all your variables, you can update the Data Element as such:

    return location.pathname.split('/').filter(Boolean).slice(0,4).join('[d]');

    Where [d] is replaced by your delimiter. Note that this will now cause the Data Element to return a single concatenated String value instead of an Array. Using %hier1% syntax in DTM fields remains the same, but you will no longer need to use .join() in Custom Code boxes.

    If your implementation uses different delimiters for different variables, implement the Data Element per the original instructions in the first section. You may use %hier1% syntax in DTM fields only if the delimiter is a comma. For all other delimiters, you will need to populate the variable in a custom code box and use a .join('[d]').

    Capturing more than Four Directory Levels

    Since you are no longer trying to put a value in four hierarchy fields, you may consider pushing more levels to hier1 or other variables.

    In the Data Element, change the 4 in .slice(0,4); to whatever max level of dirs you want to capture. Or, if you want to capture all dir levels, remove .slice(0,4) completely.