1

I have Google Analytics set up on my site and its displaying the Client Id of my visitors ok on the User Explorer Report.

I want to send the Google Analytics Client Id in a hidden field in my contact form (so I can compare users who send a form with their behaviour on my site as shown in GA).

I have used the script from the GA guide (https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id) at the top of my body code :

<script>
$( document ).ready(function() {
  ga(function(tracker) {
var clientId = tracker.get('clientId');
});
});
</script>

I hoped this would collect the client id and put it into clientId I added <?php echo"clientId" ?> to the page to check.

However, when I visit the page (https://www.cow-shed.com/cow-shed-business-start-up-pages/contact.php) it is displaying clientId rather than a number.

I have a php form which has a hidden field for clientId but this is sending clientId (not surprising probably as the echo code is also showing clientId).

This must be such a useful thing to do, but I can't see how to get the client id into my form. If anyone has any ideas. Please can you keep it simple as I'm an html coder so my pho and js is v basic ...Thank you!

Cowshed
  • 11
  • 5
  • Possible duplicate of [how to get the Google analytics client ID](https://stackoverflow.com/questions/20053949/how-to-get-the-google-analytics-client-id) – kgrg Mar 17 '19 at 20:55

1 Answers1

4

You are mixing javascript (the script part you found) and PHP and cannot do that in this way. You would need to do something like this so you are using just javascript:

$(document).ready(function() {
  ga(function(tracker) {
  var clientId = tracker.get('clientId');
  var ga_textbox = document.getElementById('textboxid'); //textbox id on form
  ga_textbox.value = clientId;
});
});

Hope that helps if you are still trying to do this.

Andrew T
  • 367
  • 2
  • 14
  • 1
    Thanks so much for answering this. I'm not still trying to do this, but its good to see what I was doing wrong. - PS I tried to vote for this but I don't have enough authority to change the visible number! – Cowshed Feb 03 '21 at 18:26