Skip to content

Commit 93e7657

Browse files
add google analytics account summaries to s3 operator
1 parent 55372cc commit 93e7657

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from airflow.plugins_manager import AirflowPlugin
22
from google_analytics_plugin.hooks.google_analytics_hook import GoogleAnalyticsHook
33
from google_analytics_plugin.operators.google_analytics_reporting_to_s3_operator import GoogleAnalyticsReportingToS3Operator
4+
from google_analytics_plugin.operators.google_analytics_account_summaries_to_s3_operator import GoogleAnalyticsAccountSummariesToS3Operator
5+
46

57
class GoogleAnalyticsPlugin(AirflowPlugin):
68
name = "google_analytics_plugin"
79
hooks = [GoogleAnalyticsHook]
8-
operators = [GoogleAnalyticsReportingToS3Operator]
10+
operators = [GoogleAnalyticsReportingToS3Operator,
11+
GoogleAnalyticsAccountSummariesToS3Operator]
912
executors = []
1013
macros = []
1114
admin_views = []
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import json
2+
import os
3+
4+
from airflow.models import BaseOperator
5+
6+
from airflow.hooks.S3_hook import S3Hook
7+
8+
from GoogleAnalyticsPlugin.hooks.google_analytics_hook import GoogleAnalyticsHook
9+
10+
11+
class GoogleAnalyticsAccountSummariesToS3Operator(BaseOperator):
12+
template_fields = ('s3_key',)
13+
14+
def __init__(self,
15+
google_analytics_conn_id,
16+
s3_conn_id,
17+
s3_bucket,
18+
s3_key,
19+
brand,
20+
space,
21+
*args,
22+
**kwargs):
23+
super().__init__(*args, **kwargs)
24+
25+
self.google_analytics_conn_id = google_analytics_conn_id
26+
self.s3_conn_id = s3_conn_id
27+
self.s3_bucket = s3_bucket
28+
self.s3_key = s3_key
29+
self.brand = brand
30+
self.space = space
31+
32+
def execute(self, context):
33+
ga_conn = GoogleAnalyticsHook(self.google_analytics_conn_id)
34+
s3_conn = S3Hook(self.s3_conn_id)
35+
36+
account_summaries = ga_conn.get_account_summaries()
37+
38+
file_name = '/tmp/{key}.jsonl'.format(key=self.s3_key)
39+
with open(file_name, 'w') as ga_file:
40+
data = []
41+
for item in account_summaries.get('items', []):
42+
root_data_obj = {
43+
'account_id': item['id'],
44+
'pgv_brand': self.brand,
45+
'pgv_space': self.space
46+
}
47+
48+
for web_property in item.get('webProperties', []):
49+
data_obj = {}
50+
data_obj.update(root_data_obj)
51+
52+
data_obj['property_id'] = web_property['id']
53+
54+
for profile in web_property.get('profiles', []):
55+
data_obj['profile_id'] = profile['id']
56+
data_obj['profile_name'] = profile['name']
57+
data.append(data_obj)
58+
59+
json_data = '\n'.join([json.dumps(d) for d in data])
60+
ga_file.write(json_data)
61+
62+
s3_conn.load_file(file_name, self.s3_key, self.s3_bucket, True)
63+
os.remove(file_name)

0 commit comments

Comments
 (0)