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

WSTEAM1-1413: Article Election Banner Experiment #12128

Merged
merged 15 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/app/components/ATIAnalytics/atiUrl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const buildATIPageTrackPath = ({
categoryName,
campaigns,
nationsProducer,
experimentVariant,
}: ATIPageTrackingProps) => {
const href = getHref(platform);
const referrer = getReferrer(platform, origin, previousPath);
Expand Down Expand Up @@ -214,6 +215,22 @@ export const buildATIPageTrackPath = ({
value: getATIMarketingString(href, campaignType),
wrap: false,
},
...(experimentVariant
? [
{
key: 'mv_test',
description: 'Article page banner experiment',
value: `Election Banner Experiment`,
wrap: false,
},
{
key: 'mv_creation',
description: 'Article page banner variant',
value: `${experimentVariant}`,
wrap: false,
},
]
: []),
...getRSSMarketingString(href, campaignType),
...(onOnionTld()
? [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const buildPageATIParams = ({
producerId,
timePublished,
timeUpdated,
experimentVariant,
} = atiData;

return {
Expand All @@ -49,6 +50,7 @@ export const buildPageATIParams = ({
statsDestination,
timePublished,
timeUpdated,
...(experimentVariant && { experimentVariant }),
};
};

Expand Down
2 changes: 2 additions & 0 deletions src/app/components/ATIAnalytics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface ATIData {
producerName?: string | null;
timePublished?: string | null;
timeUpdated?: string | null;
experimentVariant?: string | null;
}

export interface PageData {
Expand Down Expand Up @@ -131,6 +132,7 @@ export interface ATIPageTrackingProps {
categoryName?: string | null;
campaigns?: { campaignId?: string; campaignName?: string }[] | null;
nationsProducer?: string | null;
experimentVariant?: string | null;
}

export interface ATIProps {
Expand Down
4 changes: 3 additions & 1 deletion src/app/hooks/useOptimizelyMvtVariation/index.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useContext } from 'react';
import { OptimizelyContext } from '@optimizely/react-sdk';
import { RequestContext } from '#contexts/RequestContext';
import useToggle from '#hooks/useToggle';
import activateExperiment from './activateExperiment';

const useOptimizelyMvtVariation = id => {
const { optimizely } = useContext(OptimizelyContext);
const { mvtExperiments } = useContext(RequestContext);
const { enabled: electionBannerEnabled } = useToggle('electionBanner');

if (!mvtExperiments || mvtExperiments.length === 0 || id === null) {
return null;
Expand All @@ -15,7 +17,7 @@ const useOptimizelyMvtVariation = id => {
({ experimentName }) => experimentName === id,
);

if (!experiment) return null;
if (!experiment || !electionBannerEnabled) return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needs to be addressed for upcoming experiments


const isEnabled = experiment.enabled;
const variation = isEnabled && experiment.variation;
Expand Down
7 changes: 6 additions & 1 deletion src/app/hooks/useOptimizelyMvtVariation/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { renderHook } from '#app/components/react-testing-library-with-providers';
import { RequestContextProvider } from '#contexts/RequestContext';
import { ToggleContextProvider } from '#contexts/ToggleContext';
import useOptimizelyMvtVariation from '.';
import * as activateExperiment from './activateExperiment';

Expand All @@ -22,7 +23,11 @@ describe('useOptimizelyMvtVariation custom hook', () => {
pathname: 'bar',
};
const wrapper = ({ children }) => (
<RequestContextProvider {...props}>{children}</RequestContextProvider>
<RequestContextProvider {...props}>
<ToggleContextProvider toggles={{ electionBanner: { enabled: true } }}>
{children}
</ToggleContextProvider>
</RequestContextProvider>
);
return renderHook(() => useOptimizelyMvtVariation(flagId), {
wrapper,
Expand Down
2 changes: 1 addition & 1 deletion src/app/hooks/useOptimizelyVariation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useState, useEffect } from 'react';
import { useDecision } from '@optimizely/react-sdk';

const isClientSide = true;
const isClientSide = false;

// ALTHOUGH THIS FUNCTION BREAKS REACT RULES BY USING CONDITIONAL HOOKS,
// WE CAN SAFELY DO SO SINCE isClientSide IS A CONSTANT AND THEREFORE GUARANTEES THAT
Expand Down
18 changes: 16 additions & 2 deletions src/app/pages/ArticlePage/ArticlePage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/** @jsx jsx */

import { useContext } from 'react';
/** @jsxFrag React.Fragment */
import React, { useContext } from 'react';
import { jsx, useTheme } from '@emotion/react';
import useToggle from '#hooks/useToggle';
import useOptimizelyMvtVariation from '#hooks/useOptimizelyMvtVariation';
import { singleTextBlock } from '#app/models/blocks';
import ArticleMetadata from '#containers/ArticleMetadata';
import { RequestContext } from '#contexts/RequestContext';
Expand Down Expand Up @@ -39,6 +40,8 @@ import CpsRecommendations from '#containers/CpsRecommendations';
import InlinePodcastPromo from '#containers/PodcastPromo/Inline';
import { Article, OptimoBylineBlock } from '#app/models/types/optimo';
import ScrollablePromo from '#components/ScrollablePromo';
import OptimizelyArticleCompleteTracking from '#app/legacy/containers/OptimizelyArticleCompleteTracking';
import OptimizelyPageViewTracking from '#app/legacy/containers/OptimizelyPageViewTracking';
import ElectionBanner from './ElectionBanner';

import ImageWithCaption from '../../components/ImageWithCaption';
Expand Down Expand Up @@ -87,6 +90,10 @@ const ArticlePage = ({ pageData }: { pageData: Article }) => {
palette: { GREY_2, WHITE },
} = useTheme();

const experimentVariant = useOptimizelyMvtVariation(
'newswb_01_ap_banner_election',
);

const allowAdvertising = pageData?.metadata?.allowAdvertising ?? false;
const adcampaign = pageData?.metadata?.adCampaignKeyword;
const isTransliterated =
Expand Down Expand Up @@ -137,6 +144,7 @@ const ArticlePage = ({ pageData }: { pageData: Article }) => {
const atiData = {
...atiAnalytics,
...(isCPS && { pageTitle: `${atiAnalytics.pageTitle} - ${brandName}` }),
...(experimentVariant && { experimentVariant }),
};

const topStoriesContent = pageData?.secondaryColumn?.topStories;
Expand Down Expand Up @@ -305,6 +313,12 @@ const ArticlePage = ({ pageData }: { pageData: Article }) => {
mobileDivider={showTopics}
/>
)}
{experimentVariant && (
<>
<OptimizelyArticleCompleteTracking />
<OptimizelyPageViewTracking />
</>
)}
</div>
);
};
Expand Down
4 changes: 4 additions & 0 deletions src/app/pages/ArticlePage/ElectionBanner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import AmpIframe from '#app/components/AmpIframe';
import useToggle from '#app/hooks/useToggle';
import { Tag } from '#app/components/Metadata/types';
import { ServiceContext } from '#app/contexts/ServiceContext';
import useOptimizelyMvtVariation from '#app/hooks/useOptimizelyMvtVariation';
import { getEnvConfig } from '#app/lib/utilities/getEnvConfig';
import { MetadataTaggings } from '#app/models/types/metadata';
import { Services } from '#app/models/types/global';
Expand Down Expand Up @@ -35,6 +36,8 @@ export default function ElectionBanner({ aboutTags, taggings }: Props) {
const { enabled: electionBannerEnabled }: { enabled: boolean | null } =
useToggle('electionBanner');

const variation = useOptimizelyMvtVariation('newswb_01_ap_banner_election');

if (isLite) return null;

const {
Expand All @@ -57,6 +60,7 @@ export default function ElectionBanner({ aboutTags, taggings }: Props) {
!isEditoriallySensitive && validAboutTag && electionBannerEnabled;

if (!showBanner) return null;
if (variation === 'off') return null;

const {
SIMORGH_APP_ENV,
Expand Down
5 changes: 4 additions & 1 deletion src/app/pages/ArticlePage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import withOptimizelyProvider from '#app/legacy/containers/PageHandlers/withOptimizelyProvider';
import ArticlePage from './ArticlePage';
import applyBasicPageHandlers from '../utils/applyBasicPageHandlers';

export default applyBasicPageHandlers(ArticlePage);
const OptimizelyArticle = withOptimizelyProvider(ArticlePage);

export default applyBasicPageHandlers(OptimizelyArticle);
4 changes: 2 additions & 2 deletions src/server/utilities/mvtHeader/enabledExperimentsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Add enabled experiments objects inside this array in this format:
*/
const enabledExperimentList = [
{
name: 'test_2',
services: ['pidgin'],
name: 'newswb_01_ap_banner_election',
services: ['mundo', 'portuguese'],
pageTypes: ['article'],
},
];
Expand Down
Loading