How to store Google Analytics User ID in database

  • 21 January 2021
  • 3 replies
  • 33 views

Hi, I would like to store the Google Analytics User ID in the database. Due to certain rules in AdWords, we are not able to use audiences for exclusion but we can still use User ID.

How we can store Google Analytics User ID in Unbounce Database next to leads information?


3 replies

I do this with GTM and some JS. Here is an overview of how it works:

First, create a hidden field in your Unbounce form and name it GAID
Then in GTM, create a new variable (1st party cookie) name it “GAID” and the cookie ID is “_ga”
Then create a new tag, custom HTML, and add a JS that autopopulates the GAID field

Badge +1

@Luxqs assuming you are talking about GA client ID (its not called user ID), the solution will differ depending on your GA implementation method - Google Tag Manager, gtag.js or analytics.js.

Either way, you will have to utilize hidden form field (like @Kenji_Sano4 mentioned) to inject this into your lead submissions.

For Google Tag Manager, already mentioned by @Kenji_Sano4.

For analytics.js, you can use the following Javascript. Paste it after your GA tracking code

  ga(function(tracker) {
    var clientId = tracker.get('clientId');
    document.getElementById("gaid").value = clientId;
    
});

For gtag.js, you can use this script (also after your gtag script)

  window.onload = function(){
ga.getAll().forEach( (tracker) => {
  var clientId = tracker.get('clientId'); 
document.getElementById("gaid").value = clientId;
})
  }

Notice that I am using window.onload for gtag because the GTM script loads asynchronously so we’ll first need to load that before we can access ga tracker.

Also both scripts will go in the lightbox javascript if you are using pop-up form.

Good luck 😉

Hi @Kenji_Sano4,

May I know if you can advise on what the JS should be?

Thanks in advance!

Reply