|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from google.analytics.data_v1beta import BetaAnalyticsDataClient |
| 5 | +from google.analytics.data_v1beta.types import DateRange, Metric, RunReportRequest |
| 6 | + |
| 7 | +PORTAL_ID = os.environ['portal_id'] |
| 8 | +FOUNDATIONS_ID = os.environ['foundations_id'] |
| 9 | +COOKBOOKS_ID = os.environ['cookbook_id'] |
| 10 | + |
| 11 | + |
| 12 | +def _run_total_users_report(property_id): |
| 13 | + """Fetches total users for a given property ID |
| 14 | +
|
| 15 | + Args: |
| 16 | + property_id: The Google Analytics 4 property ID |
| 17 | +
|
| 18 | + Returns: |
| 19 | + int: The total number of active users |
| 20 | + """ |
| 21 | + |
| 22 | + client = BetaAnalyticsDataClient() |
| 23 | + |
| 24 | + request = RunReportRequest( |
| 25 | + property=f'properties/{property_id}', |
| 26 | + dimensions=[], |
| 27 | + metrics=[Metric(name='activeUsers')], |
| 28 | + date_ranges=[DateRange(start_date='2020-03-31', end_date='today')], |
| 29 | + ) |
| 30 | + |
| 31 | + response = client.run_report(request) |
| 32 | + |
| 33 | + total_users = 0 |
| 34 | + for row in response.rows: |
| 35 | + total_users += int(row.metric_values[0].value) |
| 36 | + |
| 37 | + return total_users |
| 38 | + |
| 39 | + |
| 40 | +def get_metrics(portal_id, foundations_id, cookbooks_id): |
| 41 | + """Retrieves total users for specified GA4 properties and writes to file if changes are significant.""" |
| 42 | + |
| 43 | + metrics_dict = {} |
| 44 | + metrics_dict['Portal'] = _run_total_users_report(str(portal_id)) |
| 45 | + metrics_dict['Foundations'] = _run_total_users_report(str(foundations_id)) |
| 46 | + metrics_dict['Cookbooks'] = _run_total_users_report(str(cookbooks_id)) |
| 47 | + |
| 48 | + return metrics_dict # Return the metrics dictionary |
| 49 | + |
| 50 | + |
| 51 | +def write_metrics(metrics_dict): |
| 52 | + """Reads existing metrics, compares for significant change, and writes to file if necessary.""" |
| 53 | + |
| 54 | + # Read existing user counts (handle potential file absence) |
| 55 | + try: |
| 56 | + with open('user_metrics.json') as f: |
| 57 | + user_data = json.load(f) |
| 58 | + except FileNotFoundError: |
| 59 | + user_data = {} |
| 60 | + |
| 61 | + # Define a threshold for significant change (adjust as needed) |
| 62 | + threshold = 100 |
| 63 | + has_significant_change = False |
| 64 | + for property, user_count in metrics_dict.items(): |
| 65 | + existing_count = user_data.get(property, 0) |
| 66 | + if abs(existing_count - user_count) > threshold: |
| 67 | + user_data[property] = user_count |
| 68 | + has_significant_change = True |
| 69 | + |
| 70 | + # Write to file if significant change detected |
| 71 | + if has_significant_change: |
| 72 | + with open('user_metrics.json', 'w') as outfile: |
| 73 | + json.dump(metrics_dict, outfile) |
| 74 | + return 1 # Signals significant change |
| 75 | + else: |
| 76 | + return 0 # Signals no significant change |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + metrics = get_metrics(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID) |
| 81 | + exit_code = write_metrics(metrics) |
| 82 | + if exit_code == 1: |
| 83 | + print('Significant change detected in user metrics.') |
| 84 | + else: |
| 85 | + print('No significant change in user metrics.') |
0 commit comments