public
Last active

  • Download Gist
user_engagement_1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<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>
user_engagement_2.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<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>
user_engagement_3.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<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>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.