Skip to content

Instantly share code, notes, and snippets.

@Garconis
Last active December 29, 2021 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Garconis/b52f22c55f42376a1789fffd407821d2 to your computer and use it in GitHub Desktop.
Save Garconis/b52f22c55f42376a1789fffd407821d2 to your computer and use it in GitHub Desktop.
Google Analytics | Get GA Client ID cookie data and other GA data on Gravity Form submit via hidden fields
// get GA Client ID when using analytics.js
// set the tracker data via the ready callback
// https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie
// more info: https://developers.google.com/analytics/devguides/collection/analyticsjs/accessing-trackers#getting_data_stored_on_a_tracker
// fields we can get with a tracker: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference
// helpers: https://www.simoahava.com/analytics/universal-analytics-plugins-explained/
// helpers: https://hevodata.com/learn/google-analytics-and-salesforce/
// check to see if the ID exists first
var gform_6_exists = document.getElementById('gform_6');
// if that variable exists, then continue...
if(gform_6_exists) {
// keep an eye on this form and trigger on form submit
gform_6_exists.addEventListener('submit', function(event) {
// uses ga object methods inside a readyCallback as they're guaranteed to be available.
ga(function() {
// get all the Google Analytics tracker data, so we can then specify which ones we want to use (by getting list of first elements in the array)
var tracker = ga.getAll()[0];
var clientId = tracker.get('clientId');
document.getElementById('input_6_6').value = clientId;
var screenResolution = tracker.get('screenResolution');
document.getElementById('input_6_7').value = screenResolution;
var viewportSize = tracker.get('viewportSize');
document.getElementById('input_6_8').value = viewportSize;
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment