1

I am setting up the code for GA User-ID on my Python template (head section). The full GA script is:

<!-- Google Analytics -->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-HIDDENFORTHISQUESTION-1', 'auto');
  ga('set', '&uid', {{ request.user.id }});
  ga('send', 'pageview');
</script>

I get this error when resquest.user.id is set to None, as no user is logged in on the site:

ga('set', '&uid', None);

When a user is logged in, I get the proper id number and no error (e.g. 8543 stands for registered user #8543):

ga('set', '&uid', 8543);

How can I avoid this error when user.id returns None?

I believe I should wrap that line in an if sentence to convert the value "None" into NULL or 0, but my JS is very limited and I don't want to mess anything up :)

Thanks in advance for your help!

RicardoX
  • 15
  • 4

1 Answers1

0

None is not a name of a function so it emits an Error.

Make condition in your Python macro:

if('{{request.user.id}}'!='None') {
  ga('set', '&uid', {{request.user.id}});
  }

PS: I dont know python, so the syntax is nonsense, but you know and this will solve your problem.

RicardoX
  • 15
  • 4
Jakub Kriz
  • 1,501
  • 2
  • 21
  • 29
  • Another very important thing I realized is that "ga('send', 'pageview');" has to go after the user ID code. Otherwise Google Analytics doesn't track it. (I've already edited the original question to reflect this) – RicardoX Jun 30 '15 at 23:17
  • Yes, all setting have to be setted up before any ga('send',...) instruction. – Jakub Kriz Jul 01 '15 at 09:25