2017-01-06 7 views
0

Googleアナリティクスのデータを整形してユーザーに表示するためのスーパーシンプルなダッシュボードを作成しようとしています。Djangoでgoogle analyticsのデータにアクセス

私はoAuth2ClientとDjango 1.10.4とPython 3.5を使用しています。

私はドキュメント内の例に従っていますが、今は非常に単純なアプリケーションを持っています。ランディングページにログインするように頼み、承認するリンクをクリックすると、Googleページが読み込まれ、あなたのGAデータを共有してください。あなたが受け入れるならば、あなたはページにリダイレクトされます。あなたがログインしている場合にのみ見ることができます。

しかし、私は実際にユーザーのデータを取得することはできません。例えば、ユーザーのアカウントのプロパティのリストや、先週のプロパティのページビューの数などを取得するにはどうすればよいでしょうか?

これは、これまでの私のコードです:

/pools/models.py

from django import http 
from oauth2client.contrib.django_util import decorators 
from django.views.generic import ListView 
# from polls.models import GoogleAnalytic 
from django.http import HttpResponse 
# Google Analytics 
from apiclient.discovery import build 

# def index(request): 
#  return http.HttpResponse("Hello and Welcome! </br> </br> Click <a href='/profile_enabled'> here</a> to login") 

@decorators.oauth_required 
def get_profile_required(request): 
    resp, content = request.oauth.http.request(
     'https://www.googleapis.com/plus/v1/people/me') 
    return http.HttpResponse(content) 

@decorators.oauth_enabled 
def get_profile_optional(request): 
    if request.oauth.has_credentials(): 
     # this could be passed into a view 
     # request.oauth.http is also initialized 
     return http.HttpResponse('You are signed in.</br> </br>'+'User email: {}'.format(
      request.oauth.credentials.id_token['email']) + "</br></br>Click <a href='/ga'> here </a> to view your metrics") 
    else: 
     return http.HttpResponse(
      'Hello And Welcome!</br></br>' 
      'You need to sign in to view your data. </br></br>' + 
      'Here is an OAuth Authorize link:<a href="{}">Authorize</a>' 
      .format(request.oauth.get_authorize_redirect())) 

########## MY CODE! ############### 

@decorators.oauth_required 
def google_analytics(object): 
    return HttpResponse('These are your results for last week:') 

urls.py

from django.conf import urls 
from polls import views 
import oauth2client.contrib.django_util.site as django_util_site 


urlpatterns = [ 
    urls.url(r'^$', views.get_profile_optional), 
    urls.url(r'^profile_required$', views.get_profile_required), 
    # urls.url(r'^profile_enabled$', views.get_profile_optional), 
    urls.url(r'^oauth2/', urls.include(django_util_site.urls)), 
    urls.url(r'^ga/$', views.google_analytics) 
] 

settings.py

[...] 


    GOOGLE_OAUTH2_CLIENT_ID = 'XXX.apps.googleusercontent.com' 

    GOOGLE_OAUTH2_CLIENT_SECRET = 'XXX' 

    GOOGLE_OAUTH2_SCOPES = ('email','https://www.googleapis.com/auth/analytics') 

Djangoがその特定のユーザーのデータにアクセスするためにトークンを保存する場所がわかりません。電子メールアドレスが正しく印刷されるなどの理由で動作しますが、わかりません実際に特定のGoogle APIメソッドを取得するには、def google_analytics(object):に追加する必要があります。

誰かがこのようなことを経験したことがありましたら、私は本当に助けに感謝します!ありがとう!

答えて

0

たとえば、Googleアナリティクスの設定の詳細を取得する場合は、次のようにします。あなたは、Googleアナリティクスビュー(別名プロファイル)から特定のdimension and metricsのデータを取得したい場合はGoogle Analytics Management API V3

を使用すると、あなたがCore Reporting API V3Analytics Reporting API V4のいずれかを使用していることを行うことができますアカウント、ウェブプロパティ、プロファイル、フィルタ、目標などを行うことができます。

Pythonのapiの例はそれぞれのガイドにあります。

関連する問題