Skip to content

Instantly share code, notes, and snippets.

@chrisle
Last active December 11, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisle/4680717 to your computer and use it in GitHub Desktop.
Save chrisle/4680717 to your computer and use it in GitHub Desktop.
<form id="myForm">
Name: <input type="text" name="name" id="name" /><br/>
Email: <input type="text" name="email" id="email" /><br/>
Password: <input type="text" name="password" id="password" /><br/>
Website: <input type="text" name="website" id="website" /><br/>
<button type="submit" onClick="trackUserEngagement('myForm');">Submit</button>
</form>
<script type="text/javascript">
// Track user engagement on a form
function trackUserEngagement(formId) {
// Loop through all the text boxes in the form
$( "form#" + formId + " :input" ).each( function () {
var
id = $(this).attr('id'), // ID of the textbox
value = $(this).val(); // Value inside the text box
// If there's something in the textbox push a tracking event to Google Analytics
if (value.length > 0) {
_gaq.push([ '_trackEvent', 'Forms:' + formId, 'Interacted with', id ]);
}
});
}
</script>
<form id="myForm">
Name: <input type="text" name="name" id="name" data-ga-label="name" /><br/>
Email: <input type="text" name="email" id="email" data-ga-label="email" /><br/>
Password: <input type="text" name="password" id="password" data-ga-label="password" /><br/>
Website: <input type="text" name="website" id="website" data-ga-label="website" /><br/>
<button type="submit">Submit</button>
</form>
<script type="text/javascript">
var _gaq = [];
$(function() {
$('input[data-ga-label]').each( function () {
$(this).click( function (e) {
var
formId = $(this).closest('form').attr('id');
id = $(this).attr('id'), // ID of the textbox
value = $(this).val(); // Value inside the text box
// Push a tracking event to Google Analytics
_gaq.push([ '_trackEvent', 'Forms:' + formId, 'Interacted with', id ]);
});
});
});
</script>
<form id="myForm">
Name: <input type="text" name="name" id="name" data-ga-label="name" /><br/>
Email: <input type="text" name="email" id="email" data-ga-label="email" /><br/>
Password: <input type="text" name="password" id="password" data-ga-label="password" /><br/>
Website: <input type="text" name="website" id="website" data-ga-label="website" /><br/>
<button type="submit">Submit</button>
</form>
<script type="text/javascript">
var _gaq = [];
$(function() {
$('input[data-ga-label]').each( function () {
// Binds the anonymous function to both click and blur
// to capture clicking and tabbing into the text box.
$(this).bind('click blur', function (e) {
var
formId = $(this).closest('form').attr('id');
id = $(this).attr('id'), // ID of the textbox
value = $(this).val(); // Value inside the text box
// Push a tracking event to Google Analytics
_gaq.push([ '_trackEvent', 'Forms:' + formId, 'Interacted with', id ]);
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment