Skip to content

Commit

Permalink
Merge pull request #4829 from GSA/metrics-fixes
Browse files Browse the repository at this point in the history
Updates to Metrics Report
  • Loading branch information
jbrown-xentity authored Jul 31, 2024
2 parents bb76031 + 94c990f commit 3e27cb1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-metrics-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build Metrics Report
on: # yamllint disable-line rule:truthy
workflow_dispatch:
schedule:
- cron: "0 0 * * *" # every day at 12am UTC
- cron: "0 0 1 * *" # runs first day of every month at 12am UTC

env:
PY_VERSION: "3.10"
Expand Down
31 changes: 17 additions & 14 deletions metrics/datagov_metrics/ga.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from datetime import datetime, timedelta
import calendar
import io
import csv

Expand All @@ -17,12 +18,14 @@
properties = analytics.properties()


def date_range(days_ago: int):
today = datetime.date.today().strftime("%Y-%m-%d")
days_ago_strfmt = (
datetime.date.today() - datetime.timedelta(days=days_ago)
).strftime("%Y-%m-%d")
return [{"startDate": days_ago_strfmt, "endDate": today}]
def date_range_last_month():
last_month = datetime.today().replace(day=1) - timedelta(days=1)
last_day = calendar.monthrange(last_month.year, last_month.month)[1]
end_date = datetime(last_month.year, last_month.month, last_day).strftime(
"%Y-%m-%d"
)
start_date = datetime(last_month.year, last_month.month, 1).strftime("%Y-%m-%d")
return [{"startDate": start_date, "endDate": end_date}]


def get_org_list():
Expand All @@ -47,7 +50,7 @@ def setup_organization_reports():

# report most viewd dataset pages per organization
org_reports[f"{org}__page_requests__last30"] = {
"dateRanges": date_range(30),
"dateRanges": date_range_last_month(),
"dimensions": [
{"name": "pagePath"},
{"name": "customEvent:DATAGOV_dataset_organization"},
Expand All @@ -60,7 +63,7 @@ def setup_organization_reports():

# report most downloaded files per organization
org_reports[f"{org}__download_requests__last30"] = {
"dateRanges": date_range(30),
"dateRanges": date_range_last_month(),
"dimensions": [
{"name": "linkUrl"},
{"name": "customEvent:DATAGOV_dataset_organization"},
Expand Down Expand Up @@ -90,7 +93,7 @@ def setup_organization_reports():

# report most clicked outboud links per organization
org_reports[f"{org}__link_requests__last30"] = {
"dateRanges": date_range(30),
"dateRanges": date_range_last_month(),
"dimensions": [
{"name": "linkUrl"},
{"name": "customEvent:DATAGOV_dataset_organization"},
Expand Down Expand Up @@ -121,7 +124,7 @@ def setup_global_reports():
global_reports = {}

global_reports["global__page_requests__last30"] = {
"dateRanges": date_range(30),
"dateRanges": date_range_last_month(),
"dimensions": [{"name": "pagePath"}],
# TODO add filter to clean up pages?
# "dimensionFilter": {},
Expand All @@ -130,12 +133,12 @@ def setup_global_reports():
}

global_reports["global__total_pageviews__last30"] = {
"dateRanges": date_range(30),
"dateRanges": date_range_last_month(),
"metrics": [{"name": "screenPageViews"}],
}

global_reports["global__top_search_terms__last30"] = {
"dateRanges": date_range(30),
"dateRanges": date_range_last_month(),
"dimensions": [{"name": "searchTerm"}],
"dimensionFilter": {
"andGroup": {
Expand Down Expand Up @@ -167,7 +170,7 @@ def setup_global_reports():
}

global_reports["global__device_category__last30"] = {
"dateRanges": date_range(30),
"dateRanges": date_range_last_month(),
"dimensions": [{"name": "deviceCategory"}],
"metrics": [{"name": "activeUsers"}],
"orderBys": [{"metric": {"metricName": "activeUsers"}, "desc": True}],
Expand Down

0 comments on commit 3e27cb1

Please sign in to comment.