Skip to content

Commit

Permalink
Add basic newsletter analytics gathering
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed Jul 26, 2023
1 parent ede6701 commit d446ac7
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
104 changes: 104 additions & 0 deletions fragdenstaat_de/fds_newsletter/analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from datetime import timedelta
from urllib.parse import urlencode

from django.conf import settings
from django.db.models import Count, Sum

import requests

from .models import Newsletter, Subscriber


def get_analytics(start_date, end_date, newsletter=None):
if newsletter is None:
newsletter = Newsletter.objects.get(slug=settings.DEFAULT_NEWSLETTER)

data = {}
subscribers = Subscriber.objects.filter(newsletter=newsletter).filter(
subscribed__date__gte=start_date, subscribed__date__lt=end_date
)
data["new_subscriber_count"] = subscribers.count()
subscriber_references = (
subscribers.values("reference")
.annotate(count=Count("reference"))
.order_by("-count")
)
for subscriber_reference in subscriber_references:
data[
"new_subscriber_count_" + subscriber_reference["reference"]
] = subscriber_reference["count"]

unsubscribers = Subscriber.objects.filter(newsletter=newsletter).filter(
unsubscribed__date__gte=start_date, unsubscribed__date__lt=end_date
)
data["unsubscriber_count"] = unsubscribers.count()
unsubscriber_references = (
unsubscribers.values("reference")
.annotate(count=Count("reference"))
.order_by("-count")
)
for unsubscriber_reference in unsubscriber_references:
data[
"unsubscriber_count_" + unsubscriber_reference["reference"]
] = unsubscriber_reference["count"]

data.update(get_donation_data(start_date, end_date, "newsletter"))
data.update(get_matomo_data(start_date, end_date, "newsletter"))
return data


def get_donation_data(start_date, end_date, reference_name):
from fragdenstaat_de.fds_donation.models import Donation

return Donation.objects.filter(
timestamp__date__gte=start_date,
timestamp__date__lt=end_date,
completed=True,
reference=reference_name,
).aggregate(
donations_total=Sum("amount"),
donations_count=Count("id"),
)


def get_matomo_data(start_date, end_date, referrer_name):
if not settings.MATOMO_API_URL:
return {}
# end date is inclusive in API
end_date = end_date - timedelta(days=1)
query_params = {
"module": "API",
"method": "Referrers.getCampaigns",
"period": "range",
"date": start_date.strftime("%Y-%m-%d") + "," + end_date.strftime("%Y-%m-%d"),
"segment": "referrerName=={}".format(referrer_name),
}
url = settings.MATOMO_API_URL + "&" + urlencode(query_params)
data = requests.get(url).json()
if not len(data):
return {}
base_data = data[0]
goal_key = "idgoal={}".format(settings.MATOMO_GOAL_ID)
goal_conversions = (
base_data.get("goals", {}).get(goal_key, {}).get("nb_conversions", 0)
)

return {
"visits": base_data["nb_visits"],
"goal_conversions": goal_conversions,
"goal_conversion_percentage": (
goal_conversions / base_data["nb_visits"]
if base_data["nb_visits"] > 0
else 0
)
* 100,
}

# query_params.update(
# {
# "method": "Referrers.getKeywordsFromCampaignId",
# "idSubtable": base_data["idsubdatatable"],
# }
# )
# url = settings.MATOMO_API_URL + "&" + urlencode(query_params)
# keyword_data = requests.get(url).json()
19 changes: 19 additions & 0 deletions fragdenstaat_de/fds_newsletter/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import json
from datetime import timedelta

from django.utils import timezone

from fragdenstaat_de.theme.notifications import send_notification

from froide.celery import app as celery_app

from .analytics import get_analytics
from .utils import cleanup_subscribers, send_onboarding_schedule


Expand All @@ -15,3 +21,16 @@ def trigger_onboarding_schedule():
now = timezone.now()
today = timezone.localdate(now)
send_onboarding_schedule(today)


@celery_app.task(name="fragdenstaat_de.fds_newsletter.send_analytics")
def send_analytics():
now = timezone.now()
week = timedelta(days=7)
start = now - week
start_day = timezone.localdate(start)
today = timezone.localdate(now)
data = get_analytics(start_day, today)
send_notification(
"📊 Newsletter Analytics\n```{}```".format(json.dumps(data, indent=4))
)
2 changes: 2 additions & 0 deletions fragdenstaat_de/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ def FROIDE_CONFIG(self):
"SLACK_DEFAULT_CHANNEL", "fragdenstaat-alerts"
)
SLACK_WEBHOOK_URL = os.environ.get("SLACK_WEBHOOK_URL", "")
MATOMO_API_URL = os.environ.get("MATOMO_API_URL", "")
MATOMO_GOAL_ID = os.environ.get("MATOMO_GOAL_ID", "7")

SENTRY_JS_URL = ""

Expand Down

0 comments on commit d446ac7

Please sign in to comment.