Analytics
2.5K members online now
2.5K 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
star_border
Reply

Capture the value of an HTML variable

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello,

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.

TIA!

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:
https://support.google.com/analytics/answer/2709828?hl=en

Modifying Pageviews (virtual)
https://developers.google.com/analytics/devguides/collection/analyticsjs/pages

CJS - GTM via Simo Ahave.. king of GTM dev. Smiley Happy
http://www.simoahava.com/analytics/variable-guide-google-tag-manager/#6

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!

Best.

Theo Bennett

Analytics Evangelist at MoreVisibility | Contact Me
Connect on LinkedIn