digital-analytics-toolkit 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,176 @@
1
+ from google.analytics.admin import AnalyticsAdminServiceClient
2
+ import pandas as pd
3
+
4
+ def get_ga4_accounts_list(credentials):
5
+ """
6
+ Retrieves a list of Google Analytics 4 (GA4) account summaries available
7
+ to the authenticated user.
8
+
9
+ Args:
10
+ credentials (google.oauth2.credentials.Credentials): The OAuth2
11
+ credentials used to authenticate the Admin API client.
12
+
13
+ Returns:
14
+ pd.DataFrame: A DataFrame containing the account resource name,
15
+ display name, and a list of property summaries for each account.
16
+ """
17
+ client = AnalyticsAdminServiceClient(credentials=credentials)
18
+
19
+ accounts = []
20
+ for account in client.list_account_summaries():
21
+ accounts.append({
22
+ "name": account.name,
23
+ "display_name": account.display_name,
24
+ "property_summaries": account.property_summaries
25
+ })
26
+
27
+ return pd.DataFrame(accounts)
28
+
29
+ def get_ga4_properties_list(credentials, account_id):
30
+ """
31
+ Retrieves detailed metadata for all GA4 properties under a specific account.
32
+
33
+ Args:
34
+ credentials (google.oauth2.credentials.Credentials): The OAuth2
35
+ credentials used to authenticate the Admin API client.
36
+ account_id (str or int): The numeric ID of the GA4 account (e.g., '123456').
37
+
38
+ Returns:
39
+ pd.DataFrame: A DataFrame containing property details including name,
40
+ display name, industry category, timezone, currency, and service level.
41
+ """
42
+
43
+ client = AnalyticsAdminServiceClient(credentials=credentials)
44
+ request = 'parent:accounts/' + str(account_id)
45
+ properties_list = client.list_properties(request={'filter':request})
46
+ properties = {
47
+ 'account' : [],
48
+ 'property': [],
49
+ 'display_name': [],
50
+ 'industry_category' : [],
51
+ 'time_zone' : [],
52
+ 'currency_code' : [],
53
+ 'service_level' : [],
54
+ 'property_type' : [],
55
+ }
56
+ for property in properties_list:
57
+ properties['account'].append(property.account)
58
+ properties['property'].append(property.name)
59
+ properties['display_name'].append(property.display_name)
60
+ properties['industry_category'].append(property.industry_category)
61
+ properties['time_zone'].append(property.time_zone)
62
+ properties['currency_code'].append(property.currency_code)
63
+ properties['service_level'].append(property.service_level)
64
+ properties['property_type'].append(property.property_type)
65
+ properties_df = pd.DataFrame(properties)
66
+ return properties_df
67
+
68
+ def get_ga4_property_keyEvents(credentials, property_id):
69
+ """
70
+ Retrieve key events configured for a Google Analytics 4 (GA4) property.
71
+
72
+ This function uses the Google Analytics Admin API to fetch all key events
73
+ associated with a specified GA4 property and returns the results as a
74
+ pandas DataFrame.
75
+
76
+ Args:
77
+ credentials: Authenticated Google API credentials object used to
78
+ initialize the AnalyticsAdminServiceClient.
79
+ property_id (str | int): The GA4 property ID for which key events
80
+ should be retrieved.
81
+
82
+ Returns:
83
+ pandas.DataFrame: A DataFrame containing the following columns:
84
+ - key_event_id (str): Full resource name of the key event.
85
+ - key_event_name (str): Name of the event marked as a key event.
86
+ - created_on (str): Timestamp when the key event was created,
87
+ formatted as '%Y-%m-%d %H:%M:%S'.
88
+ - custom_event (bool): Indicates whether the event is a custom event.
89
+
90
+ Raises:
91
+ google.api_core.exceptions.GoogleAPIError:
92
+ If the API request fails or authentication is invalid.
93
+
94
+ Example:
95
+ >>> df = get_ga4_property_keyEvents(credentials, "123456789")
96
+ >>> print(df.head())
97
+ """
98
+ client = AnalyticsAdminServiceClient(credentials=credentials)
99
+ request = 'properties/' + str(property_id)
100
+ key_events_list = client.list_key_events(parent=request)
101
+ key_events = {
102
+ 'key_event_id': [],
103
+ 'key_event_name': [],
104
+ 'created_on' : [],
105
+ 'custom_event' : []
106
+ }
107
+ for key_event in key_events_list:
108
+ key_events['key_event_id'].append(key_event.name)
109
+ key_events['key_event_name'].append(key_event.event_name)
110
+ key_events['created_on'].append(key_event.create_time.strftime("%Y-%m-%d %H:%M:%S"))
111
+ if key_event.custom:
112
+ key_events['custom_event'].append(True)
113
+ else:
114
+ key_events['custom_event'].append(False)
115
+ key_events_df = pd.DataFrame(key_events)
116
+ return key_events_df
117
+
118
+ def get_ga4_property_bq_link(credentials, property_id):
119
+ """
120
+ Retrieve BigQuery link configuration details for a Google Analytics 4 (GA4) property.
121
+
122
+ This function uses the Google Analytics Admin API to fetch all BigQuery
123
+ links associated with the specified GA4 property and returns the results
124
+ as a pandas DataFrame.
125
+
126
+ Args:
127
+ credentials: Authenticated Google credentials object used to initialize
128
+ the Analytics Admin API client.
129
+ property_id (str | int): The GA4 property ID.
130
+
131
+ Returns:
132
+ pandas.DataFrame: A DataFrame containing BigQuery link metadata with
133
+ the following columns:
134
+ - name: Resource name of the BigQuery link.
135
+ - project: Linked Google Cloud project ID.
136
+ - create_time: Timestamp when the link was created.
137
+ - daily_export_enabled: Whether daily export is enabled.
138
+ - streaming_export_enabled: Whether streaming export is enabled.
139
+ - fresh_daily_export_enabled: Whether fresh daily export is enabled.
140
+ - include_advertising_id: Whether advertising identifiers are included.
141
+ - export_streams: List of data streams included in the export.
142
+
143
+ Raises:
144
+ google.api_core.exceptions.GoogleAPICallError:
145
+ If the Analytics Admin API request fails.
146
+ google.api_core.exceptions.PermissionDenied:
147
+ If the provided credentials do not have access to the property.
148
+
149
+ Example:
150
+ >>> df = get_ga4_property_bq_link(credentials, "123456789")
151
+ >>> print(df.head())
152
+ """
153
+ client = AnalyticsAdminServiceClient(credentials=credentials)
154
+ request = 'properties/' + str(property_id)
155
+ bq_links_list = client.list_big_query_links(parent=request)
156
+ bq_links = {
157
+ 'name': [],
158
+ 'project': [],
159
+ 'create_time': [],
160
+ 'daily_export_enabled': [],
161
+ 'streaming_export_enabled': [],
162
+ 'fresh_daily_export_enabled': [],
163
+ 'include_advertising_id': [],
164
+ 'export_streams': [],
165
+ }
166
+ for bq_link in bq_links_list:
167
+ bq_links['name'].append(bq_link.name)
168
+ bq_links['project'].append(bq_link.project)
169
+ bq_links['create_time'].append(bq_link.create_time.strftime("%Y-%m-%d %H:%M:%S"))
170
+ bq_links['daily_export_enabled'].append(bq_link.daily_export_enabled)
171
+ bq_links['streaming_export_enabled'].append(bq_link.streaming_export_enabled)
172
+ bq_links['fresh_daily_export_enabled'].append(bq_link.fresh_daily_export_enabled)
173
+ bq_links['include_advertising_id'].append(bq_link.include_advertising_id)
174
+ bq_links['export_streams'].append(bq_link.export_streams)
175
+ bq_links_df = pd.DataFrame(bq_links)
176
+ return bq_links_df