AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

5.6K members online now
5.6K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
Guide Me

Capture the value of an HTML variable

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭


I want to read the value of a variable in an HTML page, and report it to Google Analytics.

The line in question is this:

<h1 id="results-heading">Your search for <span class="query"></span> returned <span class="total-results"></span> result(s).</h1>

I want to capture the value of the "query" variable.


1 Expert replyverified_user

Re: Capture the value of an HTML variable

Rising Star
# 2
Rising Star
Hey Adm D,

So this is pretty straightforward to do in Google Tag Manager (GTM). Basically you're just creating a Custom JavaScript Variable with a generic function that pulls the value.

If you are not using GTM, then your dev team could create code that would get the value and set it for your. How you set it is really a function of how you would use the data. That is it could be passed into the GATC as a virtual pageview or set as a custom dimension.

Links for your reference:
Custom Dimensions:

Modifying Pageviews (virtual)

CJS - GTM via Simo Ahave.. king of GTM dev. Smiley Happy

That said, this looks like a search result, is it? If so is the query also exposed in the address bar? If so, that could be captured in site search settings.

Hope that helps!


Theo Bennett

Analytics Evangelist at MoreVisibility | Contact Me
Connect on LinkedIn