register for Google Analytics

Python
pj/pj/context_processors.py(新規作成)
from django.conf import settingsdef google_analytics(request): tracking_id = getattr(settings, ‘GOOGLE_ANALYTICS_TRACKING_ID’, False) if not settings.DEBUG and tracking_id: return { ‘GOOGLE_ANALYTICS_TRACKING_ID’: tracking_id, } return {}
settings.py
TEMPLATES = [  {    ‘OPTIONS’:{      ‘context_processors’: [        ‘pj.context_processors.google_analytics’,      ]    }  }]
templates/ga.html
<!– Global site tag (gtag.js) – Google Analytics –><script async src=”https://www.googletagmanager.com/gtag/js?id=<自分のトラッキングID>”></script><script>  window.dataLayer = window.dataLayer || [];  function gtag(){dataLayer.push(arguments);}  gtag(‘js’, new Date());
  gtag(‘config’, ‘<自分のトラッキングID>’);</script>
templates/base.html
<link rel=”stylesheet” href=”{% static ‘css/style.css’ %}”>{% if GOOGLE_ANALYTICS_TRACKING_ID %}    {% include ‘ga.html’ %}{% endif %}

引用:https://djangobrothers.com/blogs/django_google_analytics/

BACK