Here's an example of how to authenticate and instantiate the Analytics API.
import httplib2
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
# Email of the Service Account.
SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com'
# Path to the Service Account's Private Key file.
SERVICE_ACCOUNT_PKCS12_FILE_PATH = '/path/to/<public_key_fingerprint>-privatekey.p12'
def createAnalyticsService():
f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
scope='https://www.googleapis.com/auth/analytics.readonly')
http = httplib2.Http()
http = credentials.authorize(http)
return build('analytics', 'v3', http=http)
As a result I've wrote a blog post with a solution that worked for me:
import httplib2 import os import datetime from django.conf import settings from apiclient.discovery import build from oauth2client.client import SignedJwtAssertionCredentials # Email of the Service Account. SERVICE_ACCOUNT_EMAIL = '12345@developer.gserviceaccount.com' # Path to the Service Account 's Private Key file. SERVICE_ACCOUNT_PKCS12_FILE_PATH = os.path.join( settings.BASE_DIR, '53aa9f98bb0f8535c34e5cf59cee0f32de500c82-privatekey.p12', ) def get_analytics(): f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb') key = f.read() f.close() credentials = SignedJwtAssertionCredentials( SERVICE_ACCOUNT_EMAIL, key, scope = 'https://www.googleapis.com/auth/analytics.readonly', ) http = httplib2.Http() http = credentials.authorize(http) service = build('analytics', 'v3', http = http) end_date = datetime.date.today() # 30 days ago start_date = end_date - datetime.timedelta(days = 30) data_query = service.data().ga().get( ** { 'ids': 'ga:123456', # the code of our project in Google Analytics 'dimensions': 'ga:pageTitle,ga:pagePath', 'metrics': 'ga:pageviews,ga:uniquePageviews', 'start_date': start_date.strftime('%Y-%m-%d'), 'end_date': end_date.strftime('%Y-%m-%d'), 'sort': '-ga:pageviews', }) analytics_data = data_query.execute() return analytics_data
How to get a list of most popular pages from Google Analytics in Python (Django)?,how to get a list of all views in a django application?,Get 'most popular' list in Django,How to use Django ORM to get a list by year of all articles with an article count
Use annotations:
from django.db.models
import Count
popular_events = Events.objects.annotate(attendee_count = Count('attendee')).filter(attendee_count__gt = 50)
Jan 21, 2021 • 3 min read
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOURTAGID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'YOURTAGID');
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOURTAGID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'YOURTAGID');
</script>
</head>
<body>
Hello World!
</body>
</html>
Yes, Django is an easy-to-learn framework compared to others. Having some knowledge of Python and web-working helps you to start developing with Django.,Django is the most popular Python web framework around. It makes it easy to build web apps more quickly and with less code. The demand for Django developers remains high as it's the most sought-after skill set right now.,If you’re aspiring to become a Django Developer, it’s essential to have strong knowledge of these core concepts before appearing for an interview. Through the medium of this article, we are sharing the top 60 most asked Django Interview Questions and Answers that will help you clear the interview with flying colors.,Flask and Django are the two most popular Python web frameworks. The following table lists some significant differences between Django and Flask
py - m django--version
$ django - admin startproject ABC
render(request, template_name, context = None, content_type = None, status = None, using = None)
class_name.as_view()