を移入しません、しかし、私は、私が実際に解決する方法がわからない問題が発生しましたPythonにとっては非常に新しいものです。しかしこれは「traffic1」新しいテーブルを作成し、より多くのディメンションとメトリクス、それを用いてデータを返すようにしようとした後、うまく働いたGoogle Analyticsの統合、テーブルには、私は、Pythonを使用して、私のPostgresデータベースに私のGoogle Analyticsのデータをアップロードしようとしている
if traffic_results.get('rows', []):
for row in traffic_results.get('rows'):
#print(row)
cursor.execute("""INSERT INTO traffic (yearMonth, users, sessions)
VALUES(%s, %s, %s)""", [row[0], row[1], row[2]])
else:
print('No Rows Found')
:
まず私は1つの次元とpsycopg2を使用して2つのメトリックを取得するためのコードを使用しました「行が見つかりません」のプリント
if traffic_results.get('rows', []):
for row in traffic_results.get('rows'):
#print(row)
cursor.execute("""INSERT INTO traffic0 (campaign, device, impressions, clicks, ctr)
VALUES(%s, %s, %s, %s, %s)""", [row[0], row[1], row[2], row[3], row[4]])
else:
print('No Rows Found')
は私が間違っている可能性が何上の任意のヒントをいただければと思います。ありがとうございました。
import psycopg2
import sys
from googleapiclient.errors import HttpError
from googleapiclient import sample_tools
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
#Main
def main():
# Authenticate and create the service for the Core Reporting API
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'xxxxxx.json',
['https://www.googleapis.com/auth/analytics.readonly'])
http_auth = credentials.authorize(Http())
service = build('analytics', 'v3', http=http_auth)
# Define the connection string and connect
conn_string = "xxx' port=xxx
dbname='GA' user='admin' password='password'"
conn = psycopg2.connect(conn_string)
# Open a cursor
cursor = conn.cursor()
# Run the query function using the API service
traffic_results = get_api_traffic_query(service).execute()
# Insert each row of the result set
if traffic_results.get('rows', []):
for row in traffic_results.get('rows'):
#print(row)
cursor.execute("""INSERT INTO traffic0 (campaign, device, impressions, clicks, ctr)
VALUES(%s, %s, %s, %s, %s)""", [row[0], row[1], row[2], row[3], row[4]])
else:
print('No Rows Found')
# Commit changes
conn.commit()
# Select and retrieve results
#cursor.execute("SELECT * FROM traffic0")
#records = cursor.fetchall()
#print(records)
# Close the cursor and the connection
cursor.close()
conn.close()
# Query function
def get_api_traffic_query(service):
return service.data().ga().get(
ids='ga:xxxxxx',
start_date='2017-01-01',
end_date='2017-01-31',
metrics='ga:impressions,ga:adClicks,ga:CTR',
dimensions='ga:campaign,ga:deviceCategory',
# sort='-ga:yearMonth',
# filters='ga:pagePath=~signup',
segment='sessions::condition::ga:hostname!~mongo|app|help|docs|staging|googleweblight',
start_index='1',
max_results='25')
if __name__ == '__main__':
main()
'traffic_results'は何ですか?それは辞書のように見えます。定義/作成に使用されるコードを入力してください。また、あなたの圧痕を修正してください。 – pault