This object is in archive! 

Google Analytics & USER_ID

Archived Annevar M. 10 years ago

Google has a User-ID feature that allows you to add the following to the GA code:

  1. ga(‘set’, ‘&uid’, {{USER_ID}}); // Set the user ID using signed-in user_id.

What I am looking for is how to render the User ID inside of UseResponse. I am assuming it has to be something similar to how the site_title is rendered.

  1. <?php echo Singular_Core::_('Settings')->getSetting('site_title')?>

How do I render out the User ID? I am adding this to head.phtml, BTW.

Replies (9)

photo
1

Could you please clarify the purposes of rendering user id in head.phtml?

If it's for Google Analytics then you can place any tracking code in Administration-Analytics-External Scripts section in head, so you don't need to change anything inside scripts

photo
1

I know we have been over this one before. I have never, in fact, been able to see what wonderful settings you actually have in the Analytics section of the Admin because this:

  1. Unknown error type: (8192) preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

IIRC it is because we are running PHP 5.5, more specifically 5.5.17, and this is a feature that you have already stated that will be "fixed" by the 3.x release.


But regardless of this fact, I need to add a customization to the GA code block, to support the User ID. And yes, I am choosing to put my GA code in the head of the document, not the footer. My GA block of code will look like this, when it works properly:

  1. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  2. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  3. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.insertBefore(a,m)
  4. })(window,document,'script','//http://www.google-analytics.com/analytics.js','ga');
  5. ga('create', 'UA-XXXXXXXX-X', 'auto');
  6. ga('send', 'pageview');
  7. ga('set', '&uid', {{123456789}});

So, I need to know how to write the variable for the User ID into the template for this reason.

photo
1

Hello,

The most easier way to retrieve user id in any place of code is:

  1. Singular_Core::_('Auth')->getLoggedUser()->id;

So, you can insert GA code inside your layout and inject user id as I wrote above.

Unfortunately Analytics section is not supported by PHP 5.5 and upper, but we have added PHP 5.5.* full compatibility in UseResponse 3.*.

photo
1

Thank you Paul!!

photo
1

This seems to be the correct way to handle this, for reference if anyone else wants to do the same thing:

  1. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  2. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  3. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.insertBefore(a,m)
  4. })(window,document,'script','//http://www.google-analytics.com/analytics.js','ga');
  5. ga('create', 'UA-XXXXXXXX-X', 'auto');
  6. ga('send', 'pageview');
  7. <?php if (!$this->loggedUser->isGuest()): ?>
  8. ga('set', '&uid', {{<?php echo Singular_Core::_('Auth')->getLoggedUser()->id;?>}});
  9. <?php else: ?>
  10. ga('set', '&uid', {{isGuest}});
  11. <?php endif; ?>

photo
1

It looks like I cannot edit my comment, and it also looks like there is some confusion because of the Google documentation on this, below is the correct code.


  1. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  2. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  3. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.insertBefore(a,m)
  4. })(window,document,'script','//http://www.google-analytics.com/analytics.js','ga');
  5. ga('create', 'UA-XXXXXXXX-X', 'auto');
  6. ga('send', 'pageview');
  7. <?php if (!$this->loggedUser->isGuest()): ?>
  8. ga('set', '&uid', '<?php echo Singular_Core::_('Auth')->getLoggedUser()->id;?>');
  9. <?php else: ?>
  10. ga('set', '&uid', 'isGuest');
  11. <?php endif; ?>

photo
1

Hello,

You can edit your comment within 30 minutes after adding (if you are not Agent or Admin).

I have another solution for your case how to get user id by using Javascript only:


  1. if (App.auth().userIsLoggedIn()) {
  2. App.loggedUser().getId();
  3. }

Note: you need to apply this implementation when application.js is loaded or after all minified javascripts are loaded if you are using assets compression feature. For example, you can simply apply this code inside templates/layout/header.phtml

photo
1

I was way outside the 30 minute limit ;)


That is a good solution for JS, I agree, but since the GA code is in JS, I think the PHP solution is what is needed here.

photo
1

As you wish :-)

Have a nice day!

Replies have been locked on this page!