Is the google anaytics Client ID in hubspot somewhere?


#1

Hi there,

I’m trying get the google Client ID into hubspot, so I can tie it back to analytics for offline conversions.

  1. Is hubspot already grabbing the CID from analytics already an hiding it somewhere in the api, hidden field?

  2. If not how can I grab the CID out of the google cookie an put it in a hidden field, so I can get it into hubspot?

Thanks for your help!
Steve


Google Analytics Connector
#2

Hey there Steve! You could have just emailed me, haha.

  1. Nope.

  2. Create the form field, then put the following script in the section, AFTER the Google Tag Manager and Facebook tracking codes, and BEFORE the closing tag (obviously changing the form field id in the code below):

ga(function (tracker) {
clientId = tracker.get('clientId');
$("#STEVES-FORM-FIELD-ID").val(clientId);
});

You gotta put it in the right place though, because if this Javascript runs before the analytics.js library has loaded, then the tracker object may not exist yet.

I’m always here if you need help.


#3

I am curious if anyone got this to work?

I tried placing the code above and a few variations in the footer and then in a module on the page with no success. Google tag manager is in the page head.

$( document ).ready(function() {
ga(function (tracker) {
clientId = tracker.get(‘clientId’);
$("#FIELD-ID").val(clientId);
});
});


#4

nope… gave up for now.


#5

Thanks for the help Mezar!

This code below works for me.

Of course your page where this script lives would have to have a field called 'google_client_id'. Or you can put it in the tag manager as 'Custom HTML'.

A caveat about 3% of the time the clientid isn't captured in the hidden field, anybody have any ideas?

The (window).load is there to wait for the content to load before it tries to populate the hidden field.


#6

Sorry didn't like my script tags I guess, here's the code, add script tags before and after.

$(window).load(function(){
var clientId = ga.getAll()[0].get('clientId');
$('input[name="google_client_id"]').val(clientId).change();
});


#7

Hi @steve,

Thanks for posting your solution! For future reference, you can surround multiple lines of code with three backtick marks like this:

```
<script>
$(window).load(function(){
var clientId = ga.getAll()[0].get('clientId');
$('input[name="google_client_id"]').val(clientId).change();
});
</script>
```

Which results in the following:

<script>
$(window).load(function(){
    var clientId = ga.getAll()[0].get('clientId');
    $('input[name="google_client_id"]').val(clientId).change();     
    });
</script>

#8

We are trying to impliment this within Google Tag Manager....is that possible? If so, what would triggering look like? Would we have it fire on all pages containing forms or with the "form submit" trigger? Thanks!


#9

jgardner,

It works with the tag manager. The solution below will work if you have a "Google Universal Analytics Standard Pageview Tag" setup already and a field called "google_client_id" in your form.

  1. In tag manager create a "Custom HTML Tag" and put the code below in it. Have it trigger on "All Pages" "Pageview" then it will trigger on all your pages, a little messy bit it will work for sure, or you can create a trigger with exceptions for just the pages with the forms.
    3% of the time the value won't make it into the field because the field isn't there yet when the tag fires, I'm working on solution for that hopefully.
<script>
$(window).on('load', function (){
    var clientId = ga.getAll()[0].get('clientId');
    $('input[name="google_client_id"]').val(clientId).change();       
    });
</script>

#10

Thanks Steve! Will give this a shot and test/validate. Appreciate your help and will report back :slight_smile:


#11

did this work?

would love to hear how this implementation can be best configured.


#12

Hi cre,

Yes this works, about 3% of the time the clientID won't make it into the field, my only explanation is some kind of browser plugin/privacy blocker/java script turned off on the user side. I've tested just putting hard coded text into the hidden field with java script and j query with the same result, even with a delay in seconds. So if 97% is good enough, it works :slight_smile: