2017-01-06 13 views
0

사용자에게 Google 애널리틱스 데이터의 형식을 잘 보여주기 위해 간단한 대시 보드를 만들려고합니다.장고로 Google 애널리틱스 데이터에 액세스

저는 oAuth2Client과 장고 1.10.4와 파이썬 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') 

내 문제는 장고가 특정 사용자의 데이터에 액세스하기 위해 토큰을 저장하는 곳을 실제로 이해하지 못한다는 것입니다. 전자 메일 주소를 올바르게 인쇄하기 때문에 작동한다는 것을 알고 있지만 알아 내지 못합니다. 실제로 특정 Google API 메소드를 얻으려면 def google_analytics(object):에 추가해야합니다.

누구나 이런 종류의 경험이 있으면 정말 도움이됩니다. 감사!

답변

0

Google 애널리틱스 구성 세부 정보를 가져 오려면 다음과 같이하십시오. 등 계정, 웹 속성, 프로필, 필터, 목표, 당신은 당신이 Google 웹 로그 분석보기 (일명 프로파일)에서 특정 dimension and metrics의 데이터를 가져하려면 Google Analytics Management API V3

를 사용하여, 당신이 할 수있는 것을 할 수있는 사용 중 Core Reporting API V3 또는 Analytics Reporting API V4 .

파이썬 API 예제는 해당 가이드에서 찾을 수있을 것이라고 생각합니다.

관련 문제