Pathname Tokenizer Extension
This article describes the Pathname Tokenizer extension and provides a usage example.
Prerequisites
How It Works
The Pathname Tokenizer extension uses the page variable location.pathname
to create up to eight new data layer variables, each representing a section of the URL pathname. This extension can reduce the amount of development work typically needed to update your data layer and is especially helpful for websites with descriptive pathnames.
Here’s how it works:
- The extension looks at the value in
location.pathname
and separates each text value between the slash (/
) characters. So, if the location ishttps://example.com/section/folder/sub-folder/page.html
, the extension will added the following values to your data layer as the sequentially named variables_pathname1
through_pathname8
:_pathname1="section"
_pathname2="folder"
_pathname3="sub-folder"
_pathname4="page.html"
_pathname5
_pathname6
_pathname7
_pathname8
- The extension runs in the Pre Loader scope to ensure that the values are available prior to load rules.
- The pathname values change from page to page based on the URL.
Example
Any website can use the pathname tokenizer, but sites with a logical and consistent structure reflected in the URL pathname will benefit most from this extension. This example is based on an e-commerce clothing site where the pathname values represent the site section, categories, and sub-categories of the site structure.
Here are a selection of URLs that might exist on the example site:
Page 1 - https://example.com/apparel/
Page 2 - https://example.com/apparel/women/
Page 3 - https://example.com/apparel/women/jeans/
Page 4 - https://example.com/apparel/women/jeans/skinny-jeans/
When the pathname tokenizer extension runs, the pathname is split into individual variables, one for each segment. The generated _pathname#
variables will be different from page to page depending on the pathname of each URL.
This table shows the resulting values generated by the extension for each example URL above. You can see how you could use these variables to represent the structure of the site in your data layer.
To give the pathname variables more meaningful names, use the Set Data Values extension to copy the pathname variables into new variable names, such as site_section
or site_category
.
For example, you could copy _pathname1
to site_section
, _pathname2
to site_category
, _pathname3
to site_subcategory
, and _pathname4
to site_subcategory2
, and so on.
Variable | Value on Page 1 | Value on Page 2 | Value on Page 3 | Value on Page 4 |
---|---|---|---|---|
_pathname1 |
“apparel” | “apparel” | “apparel” | “apparel” |
_pathname2 |
“women” | “women” | “women” | |
_pathname3 |
“jeans” | “jeans” | ||
_pathname4 |
“skinny-jeans” | |||
_pathname5 |
||||
_pathname6 |
||||
_pathname7 |
||||
_pathname8 |
This page was last updated: January 7, 2023