Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site-wide Donate banner A/B testing demo #12927

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

danielfmiranda
Copy link
Collaborator

@danielfmiranda danielfmiranda commented Sep 27, 2024

Description

The current Donate banner, implemented as a snippet, cannot be A/B tested because our A/B testing package only supports testing at the Page level, not for snippets or fields that aren’t directly tied to a page.

This PR enables site-wide A/B testing for the DonateBanner by implementing the following changes:

  • Added a new donate_banner field to the Homepage model and updated the homepage template to render the banner when applicable.

    • Since the Homepage is a Page model, this allows us to run A/B tests on different donate banners.
  • Updated the BasePage model with a new get_donate_banner() method and modified its template, base.html, to use this method:

    • The method retrieves the Donate banner from the Homepage.
    • If an active A/B test is running on the Homepage, the method checks if the user has already been assigned a test variant by looking for a specific cookie.
    • If no cookie is found, the user is added as a new participant in the test using the add_participant() method, which assigns them to either the "control" or "variant" group.
    • Based on their assigned group, the method selects the appropriate version of the donate banner to display. Additionally, a cookie is set to ensure the user continues to see the same version on subsequent visits.
  • Introduced a custom Wagtail A/B test event called DonateBannerLinkClick and updated the Donate banner's JavaScript to trigger this event whenever the banner's CTA button is clicked.

Notes

Here is a list of all pages that inherit from the BasePage class. If the donate banner is set on the homepage, it will render on the following pages (including during A/B tests):

  • AppInstallPage
  • ArticlePage
  • BanneredCampaignPage
  • BlogIndexPage
  • BlogPage
  • BuyersGuideArticlePage
  • BuyersGuideCampaignPage
  • BuyersGuideEditorialContentIndexPage
  • BuyersGuidePage
  • CampaignIndexPage
  • CampaignPage
  • ConsumerCreepometerPage
  • DearInternetPage
  • GeneralProductPage
  • IndexPage
  • InitiativesPage
  • MiniSiteNameSpace
  • ModularPage
  • OpportunityPage
  • ParticipatePage2
  • PrimaryPage
  • ProductPage
  • PublicationPage
  • RCCAuthorsIndexPage
  • RCCDetailPage
  • RCCLandingPage
  • RCCLibraryPage
  • ResearchAuthorsIndexPage
  • ResearchDetailPage
  • ResearchLandingPage
  • ResearchLibraryPage
  • Styleguide
  • YoutubeRegrets2021Page
  • YoutubeRegrets2022Page
  • YoutubeRegretsPage
  • YoutubeRegretsReporterPage

Screenshots

CMS:

Screenshot 2024-09-27 at 14-08-28 Editing Homepage Homepage - Wagtail

Steps to test A/B Testing

  1. Visit https://foundation-s-donate-ban-wzxauo.herokuapp.com/cms/pages/46/edit/
  2. Log in with credentials: admin2/admin2
  3. Select a new donate banner for the "donate banner" field
  4. Select "save and create AB test"
  5. On the A/B test page, select "Donate Banner Link Click" on the "what is the goal?" dropdown. Since the "donate banner link click" event is global, you do not need to choose a page for the green "Choose a page" button.
  6. Click "save and run test"
  7. You can now test that a visit to the site will serve you a variant of the donate banner, and that the AB test dashboard is accurately tracking variants served and link clicks 👍

@danielfmiranda danielfmiranda changed the title Donate banner ab testing demo [WIP] - Donate banner sitewide ab testing demo Sep 27, 2024
Comment on lines +113 to +114
{% if donate_banner %}
{% include "fragments/donate_banner.html" with banner=donate_banner.localized %}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of displaying the donate banner set in the site’s settings, we are now rendering the banner provided to the context by the get_donate_banner() method.

Comment on lines +30 to +34
{% block donate_banner %}
{% if page.donate_banner %}
{% include "fragments/donate_banner.html" with banner=page.donate_banner.localized %}
{% endif %}
{% endblock %}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating the homepage template to render the donate banner it has set for it's donate_banner field.

Comment on lines +644 to +653
class DonateBannerLinkClick(BaseEvent):
name = "Donate Banner Link Click"
requires_page = False # Set to False to create a "Global" event type that could be reached on any page


@hooks.register("register_ab_testing_event_types")
def register_donate_banner_link_click_event_type():
return {
"donate-banner-link-click": DonateBannerLinkClick,
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For more details on adding custom Wagtail A/B testing events, refer to the Wagtail A/B testing docs.

Comment on lines +48 to +50
# Check for the cookie specific to this A/B test.
test_cookie_name = f"wagtail-ab-testing_{active_ab_test.id}_version"
test_version = request.COOKIES.get(test_cookie_name)
Copy link
Collaborator Author

@danielfmiranda danielfmiranda Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the wagtail-ab-testing source code, the cookie name is defined as "wagtail-ab-testing_{ab_test.id}_version".

For reference, see the relevant source code here.


# If no cookie is found, add user as a participant in test, and set a cookie for their assigned version.
if not test_version:
test_version = active_ab_test.add_participant()
Copy link
Collaborator Author

@danielfmiranda danielfmiranda Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add_participant() method adds the user as a participant to the A/B test, and returns their assigned version of either "control" or "variant".

For reference, see the relevant source code here.

@@ -33,6 +36,37 @@ class BasePage(FoundationMetadataPageMixin, FoundationNavigationPageMixin, Page)
class Meta:
abstract = True

def get_donate_banner(self, request):
Copy link
Collaborator Author

@danielfmiranda danielfmiranda Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: If we continue forward with this implementation, we would need to check if the user has "do not track" enabled.

We might also want to check if the AB test includes the donate banner. If not, we can skip through all of this.

@danielfmiranda danielfmiranda changed the title [WIP] - Donate banner sitewide ab testing demo Site-wide Donate banner A/B testing demo Sep 27, 2024
@@ -26,7 +26,7 @@
{{ banner.subtitle }}
</p>
<div class="tw-w-full tw-pt-6">
<a class="{{ btn_class }}" href="{{ banner.cta_link }}">
<a id="banner-cta-button" class="{{ btn_class }}" href="{{ banner.cta_link }}">
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ID added for JS event tracking purposes

@danielfmiranda danielfmiranda marked this pull request as ready for review September 27, 2024 21:10
@danielfmiranda danielfmiranda temporarily deployed to foundation-s-donate-ban-wzxauo September 27, 2024 21:15 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant