简体   繁体   English

Google Analytics(分析)用户ID返回未捕获的ReferenceError:未登录用户时未定义None | 蟒蛇

[英]Google Analytics User-ID returning Uncaught ReferenceError: None is not defined when user is not logged | Python

I am setting up the code for GA User-ID on my Python template (head section). 我正在Python模板(头部)上设置GA用户ID的代码。 The full GA script is: 完整的GA脚本是:

<!-- 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: 当resquest.user.id设置为None时,由于没有用户登录该站点,我得到此错误:

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

When a user is logged in, I get the proper id number and no error (eg 8543 stands for registered user #8543): 当用户登录时,我得到正确的ID号并且没有错误(例如8543代表注册用户#8543):

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

How can I avoid this error when user.id returns None? 当user.id返回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 :) 我相信我应该在if语句中将该行换行,以将值“ None”转换为NULL或0,但是我的JS非常有限,我不想弄乱任何东西:)

Thanks in advance for your help! 在此先感谢您的帮助!

None is not a name of a function so it emits an Error. None不是函数的名称,因此会发出错误。

Make condition in your Python macro: 在您的Python宏中创建条件:

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. PS:我不知道python,所以语法是胡说八道,但是您知道,这将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM