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

[NASAA-203/204] - Create AmpExperiment component, add top stories experiment to PS articles #12048

Merged
merged 27 commits into from
Oct 21, 2024

Conversation

hotinglok
Copy link
Collaborator

@hotinglok hotinglok commented Oct 14, 2024

Resolves NASAA-203 and NASAA-204
Experiment Brief

Overall changes

Implements experimentation on AMP pages via AMP's built-in experimentation tools. Created the <AmpExperiment/> component and a config used for the NASAA Google Discover experiment. This PR also adds front-end changes based on the variants served by the config.

The front-end changes consist of inserting an edited copy of the Top Stories section into the blocks array that are always hidden and only shown on one column breakpoints for specific variants via css. The desktop behaviour for all variants should behave the same as usual, only showing the Top Stories section in a different location for specific variants.
These changes will only show on specific AMP assets for news and sport services for this PR.

No other services should be affected. These changes only affect specific assets initially to ensure the experimentation works as expected on pages served by the AMP cache and that performance isn't somehow significantly impacted.

This PR also does not have any analytics work completed yet (to be done in the next PR), hence it does not start the experiment or put it 'live' per se.

Code changes

  • Created <AmpExperiment/> component (similar to other existing AMP components e.g. AmpIframe)
    • Accepts a config containing experiment data so this component is not tied to this experiment and can be used for other AMP experiments in the future if needed.
  • Created /experimentTopStories folder in pages/ArticlePage that contains most of the code added specifically for this experiment.
    • Added a config to be used in <AmpExperiment/> for the experiment.
    • Created ExperimentTopStories that wraps a <TopStoriesSection/> component with experiment specific styling (to show/hide via <AmpExperiment/>).
    • Added helper function insertExperimentTopStories() that inserts a experimentTopStories block into the blocks array in the halfway position of the array (this is the first variant of the experiment).
      • There were discussions as to where this logic should be done. It was concluded that it can be done here for the experiment, but should there be a full implementation of this, it would of course be better done with other transformations in the simorgh-bff FABL module.
    • Added helper function enableExperimentTopStories() to check if the conditions to run insertExperimentTopStories() are fulfilled.
  • Refactored ArticlePage.tsx and ArticlePage.styles.ts to handle changes added specifically for the experiment.
    • Added isAmp and pathname to list of props retrieved from the request context.
    • Pulled topStoriesContent out early to be passed into the <ExperimentTopStories/> component in componentsToRender. This does not replace any existing logic with the Top Stories section.
    • Replaced blocks array used in articleBlocks (where a visually hidden heading block is added to blocks array) with blocksWithExperimentTopStories. This will be the same as the blocks array if functions to insert an experimentTopStories block are not run, so this should not impact anything besides the specific news and sport assets.
    • Separated topStoriesAndFeaturesSection css into topStoriesSection and featuresSection. This is so that styles that show/hide each Top Stories section will not affect anything else in the secondary column.
      • Added css for experimentTopStories blocks so that they only appear on one column breakpoints.
      • Added css to topStoriesSection so that it is hidden for specific variants on one column breakpoints.

Testing

Ensure the following assets behave as expected and does not break AMP validation.
Override links below with specific variant are formatted as such:
/service/articles/xyz.amp?renderer_env=env#amp-x-experimentName=variant
When checking other pages with services not affected by this PR, ensure that <amp-experiment/> is not present in the DOM. It should be present for the assets below:

Control (no changes to existing behaviour):

Variant name in code: control

Variant 1 (show Top Stories section halfway into the article content instead of after it):

Variant name in code: show_at_halfway

Helpful Links

AMP experiment documentation

@hotinglok hotinglok marked this pull request as ready for review October 15, 2024 11:39
Copy link
Contributor

@iainrj iainrj left a comment

Choose a reason for hiding this comment

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

Looking really nice! I've only commented on things I can speak to and nothing really around the AMP implementation which I'll leave to someone who understands that in Simorgh more deeply.

src/app/components/AmpExperiment/index.tsx Outdated Show resolved Hide resolved
Comment on lines 142 to 156
let blocksWithExperimentTopStories = blocks;
const topStoriesContent = pageData?.secondaryColumn?.topStories;
const shouldEnableExperimentTopStories =
enableExperimentTopStories({
isAmp,
service,
pathname,
}) && topStoriesContent;

if (shouldEnableExperimentTopStories) {
blocksWithExperimentTopStories = insertExperimentTopStories({
blocks,
topStoriesContent,
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion (non-blocking) Move this into the experiment top stories helper you've already created and just return either the blocks unchanged or the blocks with the top stories moved (as well as other things you might need)? This would hide the implementation detail of this experiment from anyone who is just parsing the article page code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I originally opted to keep this here since shouldEnableExperimentTopStories is needed to conditionally render <AmpExperiment/> on the correct pages too, but after fiddling with it I think your suggestion is cleaner. Refactored so that it's just one export from the helpers file that returns blocks and shouldEnableExperimentTopStories.

Comment on lines +28 to +30
const newsTestAsset = 'c6v11qzyv8po';
const newsAsset = 'cz7xywn940ro';
const sportAsset = 'cpgw0xjmpd3o';
Copy link
Contributor

Choose a reason for hiding this comment

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

Question I assume this is for testing only?

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 this initial PR yes. I thought it would be a good idea to just keep these restricted to one asset from each service we're impacting just to be extra safe. Will remove this in the following PR once we're happy with it all.

Copy link

@billsheikh1 billsheikh1 left a comment

Choose a reason for hiding this comment

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

Looks good. Leaving general comment but not on the AMP specific stuff

src/app/pages/ArticlePage/ArticlePage.tsx Outdated Show resolved Hide resolved
@@ -183,8 +203,8 @@ const ArticlePage = ({ pageData }: { pageData: Article }) => {
};

const articleBlocks = startsWithHeading
? blocks
: [visuallyHiddenBlock, ...blocks];
? transformedBlocks
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused on what's happening here. Does this always add in the experiment?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be. This was flagged by a TS error I get on line 102

Right operand of ?? is unreachable because the left operand is never nullish.

If I add something in like the below on line 205 then the unit tests pass

  const blocksToUse = shouldEnableExperimentTopStories
    ? transformedBlocks
    : blocks;

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the function now returns either the blocks unchanged or the experimentally changes to the blocks. This should probably be made more clear by updating the name of the function to de-couple it from experimentation. i.e getExperimentTopStoriesBlocks should really be getTopStoriesBlocks

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry that was just me being careless. You're right about the fix, I moved around a few things in the last refactor and completely left the check out. Re-added, should make sense now. Also missed some more renaming, but should be good now!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@iainrj I've kept the name getExperimentTopStoriesBlocks since it doesn't get the data for the original TopStoriesSection in the secondary column and because the top stories blocks in the article blocks array have type: experimentTopStories.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice one, thanks :)

Copy link
Contributor

@Isabella-Mitchell Isabella-Mitchell left a comment

Choose a reason for hiding this comment

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

Have checked test articles and can see this rendering as expected on smaller breakpoints.

Screenshot 2024-10-16 at 11 17 21
Screenshot 2024-10-16 at 11 17 28

I appreciate this is for PS users - do we need our UX/ Accessibility peers to check this? Or is that being handled by your team? 🙂

@hotinglok
Copy link
Collaborator Author

@Isabella-Mitchell I discussed this with @eagerterrier prior to starting this work and it was agreed that since it's just an experiment and there aren't any actual changes to the Top Stories section besides the position of it, we should be ok without full UX/accessibility checks.

${'only service is undefined'} | ${true} | ${'c6v11qzyv8po'} | ${undefined}
${'only service is defined and valid'} | ${false} | ${undefined} | ${'news'}
${'all props defined but service is invalid'} | ${true} | ${'c6v11qzyv8po'} | ${'igbo'}
`(

Choose a reason for hiding this comment

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

Impressive I like that

})
: blocks;
const transformedBlocks =
shouldEnableExperimentTopStories && blocks.length > 2
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added the blocks.length > 2 condition to account for:

  • The current formula for calculating the halfway position placing the experiment top stories block before the original block if there is only one block in the array.
  • The experiment top stories block being placed before any content if there were only two blocks (e.g. headline block + image block).

@HarveyPeachey HarveyPeachey merged commit e9c6b27 into latest Oct 21, 2024
11 checks passed
@HarveyPeachey HarveyPeachey deleted the nasaa-create-amp-experiment branch October 21, 2024 09:45
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.

8 participants