Skip to content

Commit

Permalink
Merge pull request #10819 from bbc/WSTEAMA-359-new-pianoanalytics-com…
Browse files Browse the repository at this point in the history
…ponent

WSTEAMA-422: Simorgh Fetches Certain ATI (Piano) Metadata Analytics for new Homepage via BFF
  • Loading branch information
alex-magana authored Jun 9, 2023
2 parents 0e5fc07 + 6df5a85 commit 599abf1
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 38 deletions.
9 changes: 8 additions & 1 deletion data/kyrgyz/homePage/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,14 @@
"visualProminence": "NORMAL",
"visualStyle": "FEED"
}
]
],
"metadata": {
"analytics": {
"contentId": "urn:bbc:tipo:topic:cm7682qz7v1t",
"contentType": "index-home",
"pageIdentifier": "kyrgyz.page"
}
}
},
"contentType": "application/json; charset=utf-8"
}
2 changes: 1 addition & 1 deletion src/app/components/ATIAnalytics/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ATI Analytics

This directory contains code for logging events to ATI Analytics.
This directory contains code for logging events to ATI (Piano) Analytics.

## Logging Click Events

Expand Down
5 changes: 3 additions & 2 deletions src/app/components/ATIAnalytics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import AmpATIAnalytics from './amp';
import { ATIProps } from './types';
import buildATIUrl from './params';

const ATIAnalytics = ({ data }: ATIProps) => {
const ATIAnalytics = ({ data, atiData }: ATIProps) => {
const requestContext = useContext(RequestContext);
const serviceContext = useContext(ServiceContext);
const { isAmp } = requestContext;

const pageviewParams = buildATIUrl(
data,
requestContext,
serviceContext,
data,
atiData,
) as string;

if (!pageviewParams) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as analyticsUtils from '#lib/analyticsUtils';
import { RequestContextProps } from '../../../../contexts/RequestContext';
import { ServiceConfig } from '../../../../models/types/serviceConfig';
import { buildPageATIParams, buildPageATIUrl } from './buildParams';

(analyticsUtils.getAtUserId as jest.Mock) = jest.fn();
(analyticsUtils.getCurrentTime as jest.Mock) = jest
.fn()
.mockReturnValue('00-00-00');
(analyticsUtils.getPublishedDatetime as jest.Mock) = jest
.fn()
.mockReturnValue('1970-01-01T00:00:00.000Z');

// @ts-expect-error - only partial data required for testing purposes
const requestContext: RequestContextProps = {
platform: 'canonical',
statsDestination: 'statsDestination',
id: 'validId',
};

// @ts-expect-error - only partial data required for testing purposes
const serviceContext: ServiceConfig = {
atiAnalyticsAppName: 'atiAnalyticsAppName',
atiAnalyticsProducerId: 'atiAnalyticsProducerId',
service: 'pidgin',
lang: 'pcm',
};

const atiData = {
analytics: {
contentId: 'urn:bbc:tipo:topic:cm7682qz7v1t',
contentType: 'index-home',
pageIdentifier: 'kyrgyz.page',
},
title: 'pageTitle',
};

const validPageURLParams = {
appName: serviceContext.atiAnalyticsAppName,
contentId: atiData.analytics.contentId,
producerId: serviceContext.atiAnalyticsProducerId,
contentType: atiData.analytics.contentType,
pageIdentifier: atiData.analytics.pageIdentifier,
pageTitle: atiData.title,
platform: requestContext.platform,
statsDestination: requestContext.statsDestination,
service: serviceContext.service,
libraryVersion: 'simorgh',
language: serviceContext.lang,
};

describe('implementation of buildPageATIParams and buildPageATIUrl', () => {
it('should return the correct object for the page given the ATI configuration', () => {
const result = buildPageATIParams({
atiData,
requestContext,
serviceContext,
});
expect(result).toEqual(validPageURLParams);
});
it('should return the correct url for a page given the ATI configuration', () => {
const result = buildPageATIUrl({
atiData,
requestContext,
serviceContext,
});
expect(result).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=kyrgyz.page&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[urn%3Abbc%3Atipo%3Atopic%3Acm7682qz7v1t]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[pcm]&x5=[http%253A%252F%252Flocalhost%252F]&x7=[index-home]&x8=[simorgh]&x9=[pageTitle]"`,
);
});
});
47 changes: 47 additions & 0 deletions src/app/components/ATIAnalytics/params/genericPage/buildParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { LIBRARY_VERSION } from '../../../../lib/analyticsUtils';
import { buildATIPageTrackPath } from '../../atiUrl';
import { ATIDataWithContexts } from '../../types';

export const buildPageATIParams = ({
atiData,
requestContext,
serviceContext,
}: ATIDataWithContexts) => {
const { platform, statsDestination } = requestContext;
const { atiAnalyticsAppName, atiAnalyticsProducerId, service, lang } =
serviceContext;
const {
analytics: {
contentId,
contentType,
pageIdentifier,
timePublished,
timeUpdated,
},
title: pageTitle,
} = atiData;
return {
appName: atiAnalyticsAppName,
contentId,
producerId: atiAnalyticsProducerId,
contentType,
pageIdentifier,
pageTitle,
platform,
statsDestination,
service,
timePublished,
timeUpdated,
libraryVersion: LIBRARY_VERSION,
language: lang,
};
};

export const buildPageATIUrl = ({
atiData,
requestContext,
serviceContext,
}: ATIDataWithContexts) =>
buildATIPageTrackPath(
buildPageATIParams({ atiData, requestContext, serviceContext }),
);
103 changes: 91 additions & 12 deletions src/app/components/ATIAnalytics/params/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { buildATIUrl, buildATIEventTrackingParams } from '.';
import { RequestContextProps } from '../../../contexts/RequestContext';
import { ServiceConfig } from '../../../models/types/serviceConfig';
import { PageData } from '../types';
import { ATIData, PageData } from '../types';

(analyticsUtils.getAtUserId as jest.Mock) = jest.fn();
(analyticsUtils.getCurrentTime as jest.Mock) = jest
Expand All @@ -41,6 +41,7 @@ const serviceContext: ServiceConfig = {
atiAnalyticsProducerId: 'atiAnalyticsProducerId',
service: 'pidgin',
brandName: 'brandName',
lang: 'pcm',
};

const article: PageData = {
Expand Down Expand Up @@ -175,13 +176,22 @@ const idxPage: PageData = {
},
};

const homePageAnalyticsData: ATIData = {
analytics: {
contentId: 'urn:bbc:tipo:topic:cm7682qz7v1t',
contentType: 'index-home',
pageIdentifier: 'kyrgyz.page',
},
title: 'pageTitle',
};

describe('ATIAnalytics params', () => {
describe('buildATIUrl', () => {
it('should return the correct article url', () => {
const url = buildATIUrl(
article,
{ ...requestContext, pageType: ARTICLE_PAGE },
serviceContext,
article,
);
expect(url).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=pidgin.articles.%2F%2Fwww.bbc.co.uk.page&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[urn%3Abbc%3Aoptimo%3Aasset%3A54321]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[language]&x5=[http%253A%252F%252Flocalhost%252F]&x6=[originhttp%253A%252F%252Fwww.example.com]&x7=[article]&x8=[simorgh]&x9=[pageTitle]&x10=[scotland]&x11=[1970-01-01T00%3A00%3A00.000Z]&x12=[1970-01-01T00%3A00%3A00.000Z]&x13=[thing%2520english%2520label%25201~thing%2520english%2520label%25202]&x14=[thing%2520id%25201~thing%2520id%25202]&x17=[thing%2520english%2520label%25201~thing%2520english%2520label%25202]&ref=originhttp://www.example.com"`,
Expand All @@ -190,9 +200,9 @@ describe('ATIAnalytics params', () => {

it('should return the correct media article url', () => {
const url = buildATIUrl(
article,
{ ...requestContext, pageType: MEDIA_ARTICLE_PAGE },
serviceContext,
article,
);
expect(url).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=pidgin.articles.%2F%2Fwww.bbc.co.uk.page&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[urn%3Abbc%3Aoptimo%3Aasset%3A54321]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[language]&x5=[http%253A%252F%252Flocalhost%252F]&x6=[originhttp%253A%252F%252Fwww.example.com]&x7=[article-sfv]&x8=[simorgh]&x9=[pageTitle]&x10=[scotland]&x11=[1970-01-01T00%3A00%3A00.000Z]&x12=[1970-01-01T00%3A00%3A00.000Z]&x13=[thing%2520english%2520label%25201~thing%2520english%2520label%25202]&x14=[thing%2520id%25201~thing%2520id%25202]&x17=[thing%2520english%2520label%25201~thing%2520english%2520label%25202]&ref=originhttp://www.example.com"`,
Expand All @@ -201,9 +211,9 @@ describe('ATIAnalytics params', () => {

it('should return the correct frontPage url', () => {
const url = buildATIUrl(
frontPage,
{ ...requestContext, pageType: FRONT_PAGE },
serviceContext,
frontPage,
);
expect(url).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=service.page&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[urn%3Abbc%3Acps%3A00000000-0000-0000-0000-000000000000]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[language]&x5=[http%253A%252F%252Flocalhost%252F]&x7=[index-home]&x8=[simorgh]&x9=[title%2520-%2520brandName]&x11=[1970-01-01T00%3A00%3A00.000Z]&x12=[1970-01-01T00%3A00%3A00.000Z]"`,
Expand All @@ -212,9 +222,9 @@ describe('ATIAnalytics params', () => {

it('should return the correct IDX page url', () => {
const url = buildATIUrl(
idxPage,
{ ...requestContext, pageType: INDEX_PAGE },
serviceContext,
idxPage,
);
expect(url).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=service.page.idxpage&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[urn%3Abbc%3Acps%3A00000000-0000-0000-0000-000000000000]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[language]&x5=[http%253A%252F%252Flocalhost%252F]&x7=[index-section]&x8=[simorgh]&x9=[title%2520-%2520brandName]&x11=[1970-01-01T00%3A00%3A00.000Z]&x12=[1970-01-01T00%3A00%3A00.000Z]"`,
Expand All @@ -223,9 +233,9 @@ describe('ATIAnalytics params', () => {

it('should return the correct media url', () => {
const url = buildATIUrl(
media,
{ ...requestContext, pageType: MEDIA_PAGE },
serviceContext,
media,
);
expect(url).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=pageIdentifier&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[id]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[language]&x5=[http%253A%252F%252Flocalhost%252F]&x7=[player-live]&x8=[simorgh]&x9=[pageTitle]"`,
Expand All @@ -234,9 +244,9 @@ describe('ATIAnalytics params', () => {

it('should return the correct MAP url', () => {
const url = buildATIUrl(
MAP,
{ ...requestContext, pageType: MEDIA_ASSET_PAGE },
serviceContext,
MAP,
);
expect(url).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=pageIdentifier&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[urn%3Abbc%3Acps%3A4d36f80b-8711-0b4e-8da0-ef76ae8ac470]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[language]&x5=[http%253A%252F%252Flocalhost%252F]&x7=[article-media-asset]&x8=[simorgh]&x9=[headline%2520-%2520brandName]&x11=[1970-01-01T00%3A00%3A00.000Z]&x12=[1970-01-01T00%3A00%3A00.000Z]&x16=[WS%20-%20Inspire%20me]&x17=[News]"`,
Expand All @@ -245,20 +255,32 @@ describe('ATIAnalytics params', () => {

it('should return the correct PGL url', () => {
const url = buildATIUrl(
PGL,
{ ...requestContext, pageType: PHOTO_GALLERY_PAGE },
serviceContext,
PGL,
);
expect(url).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=pageIdentifier&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[urn%3Abbc%3Acps%3A4d36f80b-8711-0b4e-8da0-ef76ae8ac470]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[language]&x5=[http%253A%252F%252Flocalhost%252F]&x7=[article-photo-gallery]&x8=[simorgh]&x9=[headline%2520-%2520brandName]&x11=[1970-01-01T00%3A00%3A00.000Z]&x12=[1970-01-01T00%3A00%3A00.000Z]"`,
);
});

it('should return the correct Homepage url', () => {
const url = buildATIUrl(
{ ...requestContext, pageType: HOME_PAGE },
serviceContext,
undefined,
homePageAnalyticsData,
);
expect(url).toMatchInlineSnapshot(
`"s=598285&s2=atiAnalyticsProducerId&p=kyrgyz.page&r=0x0x24x24&re=1024x768&hl=00-00-00&lng=en-US&x1=[urn%3Abbc%3Atipo%3Atopic%3Acm7682qz7v1t]&x2=[responsive]&x3=[atiAnalyticsAppName]&x4=[pcm]&x5=[http%253A%252F%252Flocalhost%252F]&x7=[index-home]&x8=[simorgh]&x9=[pageTitle]"`,
);
});

it('should have both ref parameter and x6 referrer url parameter, if referrer url exists', () => {
const atiUrl = buildATIUrl(
article,
{ ...requestContext, pageType: ARTICLE_PAGE },
serviceContext,
article,
) as string;
const params = atiUrl.split('&');

Expand All @@ -268,9 +290,9 @@ describe('ATIAnalytics params', () => {

it('should have ref parameter as the last parameter, if referrer url exists', () => {
const atiUrl = buildATIUrl(
article,
{ ...requestContext, pageType: ARTICLE_PAGE },
serviceContext,
article,
) as string;
const params = atiUrl.split('&');

Expand All @@ -279,9 +301,9 @@ describe('ATIAnalytics params', () => {

it('should not have ref and x6 parameters, if referrer url does not exist', () => {
const atiUrl = buildATIUrl(
article,
{ ...requestContext, pageType: ARTICLE_PAGE, previousPath: '' },
serviceContext,
article,
) as string;
const params = atiUrl.split('&');

Expand All @@ -293,9 +315,9 @@ describe('ATIAnalytics params', () => {
'should return empty object {} because %s page type is not supported',
pageType => {
const url = buildATIUrl(
{},
{ ...requestContext, pageType },
serviceContext,
{},
);
expect(url).toStrictEqual({});
},
Expand Down Expand Up @@ -487,6 +509,63 @@ describe('ATIAnalytics params', () => {
});
});

it('should return the correct Homepage params', () => {
const { analytics } = homePageAnalyticsData;
const homePageData = {
metadata: { analytics },
title: 'pageTitle',
pageType: 'home',
curations: [
{
summaries: [
{
type: 'article',
title:
'“Баланы сабады деп аялымды камап салышарын өзүм да билген эмесмин”. Кадамжайда токмоктолгон наристенин атасы 3\r',
firstPublished: '2023-02-02T10:06:57.156Z',
link: 'https://www.bbc.com/kyrgyz/articles/cemg3359nwro',
imageUrl:
'https://ichef.bbci.co.uk/ace/standard/{width}/cpsdevpb/2105/test/a7436f40-a2dd-11ed-9015-6935ab4fa6ca.jpg',
description: '',
imageAlt: 'test',
id: 'cemg3359nwro',
},
],
activePage: 1,
pageCount: 1,
curationId:
'urn:bbc:tipo:list:524cf34b-cac2-4ce2-ac64-53eb70019202',
curationType: 'tipo-curation',
position: 1,
title: 'Редактордун тандоосу',
visualProminence: 'HIGH',
visualStyle: 'COLLECTION',
},
],
description: 'Hello I am a description!',
};
const params = buildATIEventTrackingParams(
homePageData,
{ ...requestContext, pageType: HOME_PAGE },
serviceContext,
);
expect(params).toEqual({
appName: 'atiAnalyticsAppName',
categoryName: undefined,
campaigns: undefined,
contentId: 'urn:bbc:tipo:topic:cm7682qz7v1t',
contentType: 'index-home',
language: 'pcm',
pageIdentifier: 'kyrgyz.page',
pageTitle: 'pageTitle',
libraryVersion: 'simorgh',
platform: 'canonical',
producerId: 'atiAnalyticsProducerId',
service: 'pidgin',
statsDestination: 'statsDestination',
});
});

it.each([ERROR_PAGE, HOME_PAGE, LIVE_PAGE])(
'should return empty object {} because %s page type is not supported',
pageType => {
Expand Down
Loading

0 comments on commit 599abf1

Please sign in to comment.