如何让 Google 标签管理器执行 python 和 Django 代码?
How can I get Google Tag Manager to execute python and Django code?
我正在尝试用 Google 跟踪代码管理器 (GTM) 代码替换 Google 分析 (GA) 代码。该代码旨在识别我们系统中具有用户名的用户。
在我当前的 GA 代码中,我通过 Django 调用了一个文件:
{{ google_analytics }}
提取这段代码:
def google_analytics(request):
disabled = {"google_analytics": ""}
# don't track internal users
if request.user and request.user.is_authenticated:
if request.user.email and request.user.email.lower().endswith('@trellis.law'):
return disabled
if request.user.username in lawbook.config.developers:
return disabled
if request.user.username in lawbook.config.blog_writers:
return disabled
if request.user.username in lawbook.config.bio_writers:
return disabled
if request.user.is_staff or request.user.is_superuser:
return disabled
# don't track in local development environment
if settings.DEBUG:
return disabled
# don't track in staging
if lawbook.config.hostname.startswith('staging'):
return disabled
if request.user and request.user.is_authenticated:
username = request.user.username
else:
username = None
context = {
"google_analytics_key": settings.GOOGLE_ANALYTICS_KEY,
"username": username
}
return {
'google_analytics': render_to_string("google_analytics.html", context)
}
我需要做的就是设置一个在每个页面上执行此操作的标记。我该怎么做?
不确定公开共享您的密钥的想法有多好,但是,这是 django 对您提出的问题的解决方案。
def google_analytics_context_processor(request):
# rest of your code
return {'google_analytics': render_to_string("google_analytics.html", context)}
然后在您的设置中
TEMPLATES = [
{
...,
'OPTIONS': {
'context_processors': [
...,
'dot.path.to.google_analytics_context_processor',
],
},
},
]
然后在您的函数视图中(class 基于视图自动处理)您使用
from django.template.context import RequestContext
def view(request):
custom_context = {'custom': 'context'}
context = RequestContext(request, custom_context)
# now where-ever you want, you use this context
# and your context from the context_processors added
# in your new context
我正在尝试用 Google 跟踪代码管理器 (GTM) 代码替换 Google 分析 (GA) 代码。该代码旨在识别我们系统中具有用户名的用户。
在我当前的 GA 代码中,我通过 Django 调用了一个文件:
{{ google_analytics }}
提取这段代码:
def google_analytics(request):
disabled = {"google_analytics": ""}
# don't track internal users
if request.user and request.user.is_authenticated:
if request.user.email and request.user.email.lower().endswith('@trellis.law'):
return disabled
if request.user.username in lawbook.config.developers:
return disabled
if request.user.username in lawbook.config.blog_writers:
return disabled
if request.user.username in lawbook.config.bio_writers:
return disabled
if request.user.is_staff or request.user.is_superuser:
return disabled
# don't track in local development environment
if settings.DEBUG:
return disabled
# don't track in staging
if lawbook.config.hostname.startswith('staging'):
return disabled
if request.user and request.user.is_authenticated:
username = request.user.username
else:
username = None
context = {
"google_analytics_key": settings.GOOGLE_ANALYTICS_KEY,
"username": username
}
return {
'google_analytics': render_to_string("google_analytics.html", context)
}
我需要做的就是设置一个在每个页面上执行此操作的标记。我该怎么做?
不确定公开共享您的密钥的想法有多好,但是,这是 django 对您提出的问题的解决方案。
def google_analytics_context_processor(request):
# rest of your code
return {'google_analytics': render_to_string("google_analytics.html", context)}
然后在您的设置中
TEMPLATES = [
{
...,
'OPTIONS': {
'context_processors': [
...,
'dot.path.to.google_analytics_context_processor',
],
},
},
]
然后在您的函数视图中(class 基于视图自动处理)您使用
from django.template.context import RequestContext
def view(request):
custom_context = {'custom': 'context'}
context = RequestContext(request, custom_context)
# now where-ever you want, you use this context
# and your context from the context_processors added
# in your new context