Retrieving value of hidden input DOM element in Google Tag Manager (GTM)

When implementing event tracking of user comments using Google Tag Manager for WordPress I came across a pretty strange behavior when trying to retrieve the value of a hidden <input> field holding the current comment post id (comment_post_ID) that should serve as event label for Google (Universal) Analytics.

WordPress Comment Form

Below you find the WordPress comment form used by most templates today. Pay attention to line 11 that shows the hidden input field comment_post_ID. This field’s value should be used as event label.

<form id="commentform" class="comment-form" action="http://www.yourdomain.com/wp-comments-post.php" method="post" novalidate="">
<span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span> 
<label for="author">Name <span class="required">*</span></label> 
<input id="author" name="author" size="30" type="text" value="" /> 
<label for="email">Email <span class="required">*</span></label> 
<input id="email" name="email" size="30" type="email" value="" /> 
<label for="url">Website</label> <input id="url" name="url" size="30" type="url" value="" /> 
<label for="comment">Comment</label> 
<textarea id="comment" cols="45" name="comment" rows="8"></textarea> 
<input id="submit" class="submit" name="submit" type="submit" value="Send your comment" /> 
<input id="comment_post_ID" name="comment_post_ID" type="hidden" value="SOME_ID" /> 
<input id="comment_parent" name="comment_parent" type="hidden" value="0" /> 
<input id="akismet_comment_nonce" name="akismet_comment_nonce" type="hidden" value="SOME_HASH" /> 
<input id="ak_js" name="ak_js" type="hidden" value="SOME_HASH" />
</form>

Retrieve value using Google Tag Manager

Per definition, Google Tag Manager evaluates DOM element variables based on an Element ID and optionally on an Attribute value. In case no Attribute Name is specified the value attribute will be used by default:

If the attribute name is set, the variable’s value will be that of the DOM element attribute; otherwise, the variable’s value will be the text of the DOM element.

Unfortunately, this does not seem to (always) work for hidden input fields. Therefore, make sure to specify the Attribute Name value manually: Google Tag Manager DOM Element variable Although, you can also use Custom JavaScript variables to achieve the same DOM element variables are clearly the way to go here.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *