I need to show in Google Analytics the values that the user enters in the fields. But I do not need to see the number of clicks on the field. Help me please.
Asked
Active
Viewed 2,334 times
2
-
Tracking text field values in GA is usually a terrible idea, since users may enter personally identifiable data which you are not allowed to store in GA per Google's terms of service. Can you elaborate on your use case ? Implementing this would be fairly trivial, but I don't want really want to make an answer if implementing this will potentially get you in trouble. – Eike Pierstorff Sep 05 '17 at 08:06
-
1Thanks for the answer, Eike . My form is a simple custom calculator. No secret data is not entered there. I need to know what numbers users enter. I need this for statistics. – Max Lutsenko Sep 05 '17 at 10:40
2 Answers
1
Sending Data to Google Programmatically
Google has two major analytics implementations: analytics.js & gtag.js - depending on which you're using, there's a different syntax for each to log page views. You could also probably log an event as well.
Analytics.js - page views
ga('send', 'pageview', [page], [fieldsObject]);
Gtag.js - pageviews
gtag('config', 'GA_MEASUREMENT_ID', {
'page_title' : 'homepage',
'page_path': '/home'
});
Handling the Event in Javascript
Your use case will determine which events you'd like to use to trigger logging. Handling the blur
event is fine for most use cases, but you also might want to handle text change and then debounce
Debouncing
Here's a discussion and vanilla js implementation of debouncing based off underscore:
function debounce(func, wait) {
var timeoutId;
return function() {
var context = this,
args = arguments;
clearTimeout(timeoutId);
timeoutId = setTimeout(function() {
func.apply(context, args);
}, wait);
};
};
Example in Stack Snippets
// generate a debounced version with a min time between calls of 2 seconds
let sendPageView = debounce(function() {
gtag('config', 'GA_MEASUREMENT_ID', {
'page_title' : 'homepage',
'page_path': '/home?searchText=' + encodeURI(filter.value)
});
console.log("Text: " + filter.value)
}, 2000)
// attach event listener
let filter = document.getElementById("filter")
filter.addEventListener('input', sendPageView)
function debounce(func, wait) {
var timeoutId;
return function() {
var context = this,
args = arguments;
clearTimeout(timeoutId);
timeoutId = setTimeout(function() {
func.apply(context, args);
}, wait);
};
};
// fake gtag for demo
gtag = function() {}
<input type="text" id="filter" />

KyleMit
- 30,350
- 66
- 462
- 664
0
You could probably combine the approach here: How to get text of an input text box during onKeyPress?
With a ga event send.

Iskandar Reza
- 953
- 1
- 7
- 16