Skip to content

Commit

Permalink
feat(YiH): add survey badge (#19163)
Browse files Browse the repository at this point in the history
* feat(YiH): add survey badge

* add all badges to the page
  • Loading branch information
pauldambra authored Dec 7, 2023
1 parent ba63e41 commit ecb111e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 8 deletions.
53 changes: 51 additions & 2 deletions posthog/year_in_posthog/2023.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
text-align: center;
}

h2 {
margin: 1rem 0 0;
}

.flex {
display: flex;
}
Expand Down Expand Up @@ -188,7 +192,7 @@
width: 100vw;
display: flex;
flex-direction: row;
z-index:9999;
z-index: 9999;
align-items: center;
}

Expand Down Expand Up @@ -257,6 +261,11 @@
height: 326px;
}

.badge.mini {
width: 163px;
height: 163px;
}

.stats {
display: flex;
flex-direction: row;
Expand All @@ -272,6 +281,30 @@
font-size: 1.5rem;
}


.achievements-wrapper {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 100%;
row-gap: 1rem;
}

.achievements {
display: flex;
flex-direction: row;
justify-content: space-between;
width: 100%;
row-gap: 1rem;
}

.achievement {
display: flex;
flex-direction: column;
align-items: center;
font-size: 1.5rem;
}

.sharing {
display: flex;
flex-direction: row;
Expand Down Expand Up @@ -374,12 +407,27 @@ <h1 class="highlight text-3xl">{{ human_badge }}</h1>
</div>
{% endfor %}
</div>
<div class="achievements-wrapper pt-4">
<h2 class="highlight text-2xl">Achievements unlocked</h2>
<div class="achievements">
{% for _, b in badges.items %}
<div class="achievement">
<picture class="badge mini">
<source type="image/webp" srcset="{% static b.image_webp %}" width="163" height="163">
<source type="image/png" srcset="{% static b.image_png %}" width="163" height="163">
<img class="badge mini" src="{% static b.image_png %}" width="163" height="163"
alt="An illustration of a hedgehog who is a {{ b.human_badge }}">
</picture>
<div class="my-4">{{ b.human_badge }}</div>
</div>
{% endfor %}
</div>
</div>
<div class="sharing">{% include "sharing-buttons.html" %}</div>
</div>
</main>
<footer class="footer" role="contentinfo">
<div class="pl-8 text-xl grow items-center flex">
<span>My year in&nbsp;&nbsp;</span>
{# copied from <FriendlyLogo/>#}
<svg
fill="none"
Expand Down Expand Up @@ -431,6 +479,7 @@ <h1 class="highlight text-3xl">{{ human_badge }}</h1>
<path d="m120.815 23.0299c1.113 0 2.105-.3846 2.631-1.0525v.9715c0 1.1334-.911 1.9228-2.307 1.9228-.992 0-1.862-.4858-2.004-1.3156l-2.712.425c.364 1.9228 2.328 3.2181 4.716 3.2181 3.137 0 5.222-1.8418 5.222-4.5741v-9.4721h-2.935v.8703c-.546-.6274-1.477-1.0322-2.672-1.0322-2.833 0-4.614 1.943-4.614 5.0194s1.781 5.0194 4.675 5.0194zm-1.781-5.0194c0-1.518.891-2.4895 2.267-2.4895 1.396 0 2.287.9715 2.287 2.4895 0 1.5179-.891 2.4894-2.287 2.4894-1.376 0-2.267-.9715-2.267-2.4894z"/>
</g>
</svg>
<span>&nbsp;unwrapped</span>
</div>
<div>
<div class="sharing">{% include "sharing-buttons.html" %}</div>
Expand Down
22 changes: 21 additions & 1 deletion posthog/year_in_posthog/calculate_2023.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@
AND created_by_id = (select id from posthog_user where uuid = %(user_uuid)s)
GROUP BY
created_by_id
),
survey_stats AS (
SELECT
created_by_id as user_id,
count(*) as survey_created_count,
CASE
WHEN count(*) >= 1 THEN 'reporter'
END AS badge
FROM
posthog_survey
WHERE
NOT created_by_id IS NULL
AND date_part('year', created_at) = 2023
AND created_by_id = (select id from posthog_user where uuid = %(user_uuid)s)
group by
created_by_id
)
SELECT
id,
Expand All @@ -98,6 +114,7 @@
recording_viewed_stats.badge,
experiments_stats.badge,
dashboards_created_stats.badge,
survey_stats.badge,
case when
recording_viewed_stats.badge is not null
and flag_stats.badge is not null
Expand All @@ -110,14 +127,16 @@
flag_stats.flag_created_count,
recording_viewed_stats.viewed_recording_count,
experiments_stats.experiments_created_count,
dashboards_created_stats.dashboards_created_count
dashboards_created_stats.dashboards_created_count,
survey_stats.survey_created_count
FROM
posthog_user
LEFT JOIN insight_stats ON posthog_user.id = insight_stats.user_id
LEFT JOIN flag_stats ON posthog_user.id = flag_stats.user_id
LEFT JOIN recording_viewed_stats ON posthog_user.id = recording_viewed_stats.user_id
LEFT JOIN experiments_stats ON posthog_user.id = experiments_stats.user_id
LEFT JOIN dashboards_created_stats ON posthog_user.id = dashboards_created_stats.user_id
LEFT JOIN survey_stats ON posthog_user.id = survey_stats.user_id
WHERE
posthog_user.uuid = %(user_uuid)s
"""
Expand Down Expand Up @@ -146,6 +165,7 @@ def calculate_year_in_posthog_2023(user_uuid: str) -> Optional[Dict]:
"viewed_recording_count": row["viewed_recording_count"],
"experiments_created_count": row["experiments_created_count"],
"dashboards_created_count": row["dashboards_created_count"],
"surveys_created_count": row["survey_created_count"],
},
"badges": row["badges"],
}
Expand Down
33 changes: 28 additions & 5 deletions posthog/year_in_posthog/year_in_posthog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
from posthog import settings
from posthog.year_in_posthog.calculate_2023 import calculate_year_in_posthog_2023


logger = structlog.get_logger(__name__)

badge_preference = ["astronaut", "deep_diver", "curator", "flag_raiser", "popcorn_muncher", "scientist", "champion"]
badge_preference = [
"astronaut",
"deep_diver",
"curator",
"flag_raiser",
"popcorn_muncher",
"scientist",
"reporter",
"champion",
]

human_badge = {
"astronaut": "Astronaut",
Expand All @@ -23,6 +31,7 @@
"flag_raiser": "Flag Raiser",
"popcorn_muncher": "Popcorn muncher",
"scientist": "Scientist",
"reporter": "Reporter",
"champion": "Champion",
}

Expand All @@ -33,6 +42,7 @@
"flag_raiser": "#FF906E",
"popcorn_muncher": "#C5A1FF",
"scientist": "#FFD371",
"reporter": "#F63C00",
"champion": "#FE729D",
}

Expand All @@ -43,6 +53,7 @@
"flag_raiser": "You've raised so many feature flags we've started to suspect that semaphore is your first language. Keep it up!",
"popcorn_muncher": "You're addicted to reality TV. And, by reality TV, we mean session recordings. You care about the UX and we want to celebrate that!",
"scientist": "You’ve earned this badge from your never ending curiosity and need for knowledge. One result we know for sure, you are doing amazing things. ",
"reporter": "This just in: You love asking questions and have a nose for news. Surveys have made you the gossip columnist of PostHog!",
"champion": "You're unmatched. Unstoppable. You're like the Usain Bolt of hedgehogs! We're grateful to have you as a PostHog power user.",
}

Expand All @@ -54,7 +65,7 @@ def stats_for_badge(data: Dict, badge: str) -> List[Dict[str, Union[int, str]]]:
if badge == "astronaut" or badge == "deep_diver":
return (
[{"count": stats["insight_created_count"], "description": "Insights created"}]
if stats["insight_created_count"]
if stats.get("insight_created_count")
else []
)
elif badge == "curator":
Expand All @@ -65,6 +76,8 @@ def stats_for_badge(data: Dict, badge: str) -> List[Dict[str, Union[int, str]]]:
return [{"count": stats["viewed_recording_count"], "description": "Session recordings viewed"}]
elif badge == "scientist":
return [{"count": stats["experiments_created_count"], "description": "Experiments created"}]
elif badge == "reporter":
return [{"count": stats["surveys_created_count"], "description": "Surveys created"}]
elif badge == "champion":
return [
{"count": stats["insight_created_count"], "description": "Insights created"},
Expand Down Expand Up @@ -93,13 +106,22 @@ def render_2023(request, user_uuid: str) -> HttpResponse:
try:
data = calculate_year_in_posthog_2023(user_uuid)

badge = sort_list_based_on_preference(data["badges"] or ["astronaut"])
badge = sort_list_based_on_preference(data.get("badges") or ["astronaut"])
except Exception as e:
# because Harry is trying to hack my URLs
logger.error("year_in_posthog_2023_error_loading_data", exc_info=True, exc=e, data=data or "no data")
capture_exception(e)
badge = "astronaut"
data = data or {"stats": {}}
data = data or {"stats": {}, "badges": []}

badge_images = {}
for b in data.get("badges") or ["astronaut"]:
if b != badge:
badge_images[b] = {
"human_badge": human_badge.get(b),
"image_png": f"year_in_hog/badges/2023_{b}.png",
"image_webp": f"year_in_hog/badges/2023_{b}.webp",
}

try:
stats = stats_for_badge(data, badge)
Expand All @@ -108,6 +130,7 @@ def render_2023(request, user_uuid: str) -> HttpResponse:
"debug": settings.DEBUG,
"api_token": os.environ.get("DEBUG_API_TOKEN", "unknown") if settings.DEBUG else "sTMFPsFhdP1Ssg",
"badge": badge,
"badges": badge_images,
"human_badge": human_badge.get(badge),
"highlight_color": highlight_color.get(badge),
"image_png": f"year_in_hog/badges/2023_{badge}.png",
Expand Down

0 comments on commit ecb111e

Please sign in to comment.