In Tag Manager, we can use variables like custom JavaScript, random number, URL and more. The list is long, so we’ll pick one type and use it as an example to show you how variables work.
A DOM element variable lets you access data on your site or app through HTML DOM (Document Object Model). You can use an element ID, CSS selector or XPath to point to the element from which you want to collect information. In our example, we’d like to capture information about a blog author on our site. Then we’d like to pass that information to Analytics to see blog post statistics by the author. Let’s see how all of that can be done.
To use a DOM element variable, follow these steps:
- Open your site on a page with a blog author name.
- Right-click on the blog author and click Inspect or Inspect element, depending on the browser you’re using.
- Look for the ID or class of the author element among the lines of code.
- The code could look like this:
<span class="author">John Doe</span>
. In this case, we’ve got the classauthor
. - Go to Menu > Tag Manager.
- Navigate to Variables.
- Click Add a variable.
- Name the variable. Example:
Blog author
. - Select the following variable type: DOM element.
- In Variable settings, set Element ID or CSS selector. In our example, we’ll use a CSS selector
.author
. - Check Extract text from an element. This option will extract the blog author’s name from the code:
<span class="author">John Doe</span>
. - Click Save.
- Click Debug.
- Go to the page with the blog author’s name.
- Navigate to Events log in debug mode.
- Click stg.PageView on the event list.
- Navigate to Variables.
- Look for your variable on the list and see if the value is assigned correctly.
- If everything looks okay, go back to Tag Manager and click Publish.
Done! Your variable is ready. Now we need to complete another step and send variable data to Analytics.
Add a custom dimension
To see statistics for a blog author on our site, we now need to pass variable data to Analytics using a custom dimension.
To add and set up a custom dimension, follow these steps:
- Go to Menu > Analytics.
- Navigate to Settings.
- On the left, click Custom dimensions.
- In Event dimensions, click Add an event dimension.
- Name the dimension.
- Click Save. And a screen will appear with instructions on how to set values for the custom dimension, which you can close.
- After creating the custom dimension, note Dimension name or ID.
- Go to Menu > Tag Manager.
- Navigate to Tags.
- On the left, click Piwik PRO. This is the tag with the tracking code.
- In Collect data for custom dimensions, click + Add a dimension.
- Enter Dimension name or ID from step 7.
- Enter Dimension value. This is our variable
{{ Blog author }}
. - Scroll down to the bottom and click Save.
- Click Publish.
- You can quickly check if data is being collected correctly by going to Menu > Analytics > Settings > Tracker debugger.
Note: Data in the tracker debugger appears instantly. Data in reports appears in about 30 minutes.
- After data is collected, you’ll see it under Menu > Analytics > Reports > Custom dimensions.
- You can also see your custom dimension under Analytics > Reports > Session log.
Tip: You can also use your custom dimension in a custom report.