Skip to content

Commit

Permalink
Brave News: each promoted item rendered in the feed should generate a…
Browse files Browse the repository at this point in the history
… unique ID for the purposes of ad service view and visit reporting
  • Loading branch information
petemill committed Sep 25, 2021
1 parent 2cb29ce commit fcee371
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
7 changes: 6 additions & 1 deletion components/brave_new_tab_ui/actions/today_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ export const errorGettingDataFromBackground = createAction<BackgroundErrorPayloa
export type ReadFeedItemPayload = {
item: BraveToday.FeedItem,
isPromoted?: boolean,
promotedUUID?: string,
openInNewTab?: boolean
}
export const readFeedItem = createAction<ReadFeedItemPayload>('readFeedItem')

export const feedItemViewedCountChanged = createAction<number>('feedItemViewedCountChanged')

export const promotedItemViewed = createAction<BraveToday.PromotedArticle>('promotedItemViewed')
export type PromotedItemViewedPayload = {
item: BraveToday.PromotedArticle
uuid: string
}
export const promotedItemViewed = createAction<PromotedItemViewedPayload>('promotedItemViewed')

export type VisitDisplayAdPayload = {
ad: BraveToday.DisplayAd
Expand Down
8 changes: 4 additions & 4 deletions components/brave_new_tab_ui/async/today.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ handler.on<Actions.ReadFeedItemPayload>(Actions.readFeedItem.getType(), async (s
]
if (payload.isPromoted) {
backendArgs.push(
payload.item.url_hash,
payload.promotedUUID,
(payload.item as BraveToday.PromotedArticle).creative_instance_id,
payload.isPromoted
)
Expand All @@ -90,10 +90,10 @@ handler.on<Actions.ReadFeedItemPayload>(Actions.readFeedItem.getType(), async (s
}
})

handler.on<BraveToday.PromotedArticle>(Actions.promotedItemViewed.getType(), async (store, item) => {
handler.on<Actions.PromotedItemViewedPayload>(Actions.promotedItemViewed.getType(), async (store, payload) => {
chrome.send('todayOnPromotedCardView', [
item.creative_instance_id,
item.url_hash
payload.item.creative_instance_id,
payload.uuid
])
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@ import { getLocale } from '../../../../../../common/locale'
import * as Card from '../../cardSizes'
import useScrollIntoView from '../../useScrollIntoView'
import useReadArticleClickHandler from '../../useReadArticleClickHandler'
import { OnReadFeedItem, OnSetPublisherPref } from '../../'
import { OnPromotedItemViewed, OnReadFeedItem, OnSetPublisherPref } from '../../'
import CardImage from '../CardImage'
import PublisherMeta from '../PublisherMeta'
// TODO(petemill): Large and Medium article should be combined to 1 component.

interface Props {
content: (BraveToday.Article | BraveToday.PromotedArticle | undefined)[]
publishers: BraveToday.Publishers
articleToScrollTo?: BraveToday.FeedItem
type Props = {
onReadFeedItem: OnReadFeedItem
onSetPublisherPref: OnSetPublisherPref
onItemViewed?: (item: BraveToday.FeedItem) => any
onItemViewed?: OnPromotedItemViewed
isPromoted?: boolean
}

type ArticleProps = {
type ArticlesProps = Props & {
content: (BraveToday.Article | BraveToday.PromotedArticle | undefined)[]
publishers: BraveToday.Publishers
articleToScrollTo?: BraveToday.FeedItem
}

type ArticleProps = Props & {
item: BraveToday.Article | BraveToday.PromotedArticle
publisher?: BraveToday.Publisher
shouldScrollIntoView?: boolean
onReadFeedItem: OnReadFeedItem
onSetPublisherPref: OnSetPublisherPref
onItemViewed?: (item: BraveToday.FeedItem) => any
isPromoted?: boolean
}

const promotedInfoUrl = 'https://brave.com/brave-today'
Expand All @@ -50,12 +49,23 @@ const LargeArticle = React.forwardRef<HTMLElement, ArticleProps>(function (props
const { publisher, item } = props
const [cardRef] = useScrollIntoView(props.shouldScrollIntoView || false)

const onClick = useReadArticleClickHandler(props.onReadFeedItem, { item, isPromoted: props.isPromoted })

const innerRef = React.useRef<HTMLElement>(null)

const onItemViewedRef = React.useRef(props.onItemViewed)
const uuid = React.useMemo<string | undefined>(function () {
if (props.isPromoted) {
// @ts-ignore
const uuid: string = crypto.randomUUID()
return uuid
}
return undefined
}, [props.isPromoted, props.item.url])

const onClick = useReadArticleClickHandler(props.onReadFeedItem, { item, isPromoted: props.isPromoted, promotedUUID: uuid })

const onItemViewedRef = React.useRef<Function | null>()
onItemViewedRef.current = props.onItemViewed
? props.onItemViewed.bind(undefined, { item: props.item, uuid })
: null

React.useEffect(() => {
if (!innerRef.current) {
Expand All @@ -79,7 +89,7 @@ const LargeArticle = React.forwardRef<HTMLElement, ArticleProps>(function (props
const observer = new VisibilityTimer(() => {
const onItemViewed = onItemViewedRef.current
if (onItemViewed) {
onItemViewed(item)
onItemViewed()
}
}, 100, innerRef.current)

Expand Down Expand Up @@ -136,7 +146,7 @@ const LargeArticle = React.forwardRef<HTMLElement, ArticleProps>(function (props
)
})

const CardSingleArticleLarge = React.forwardRef<HTMLElement, Props>(function (props, ref) {
const CardSingleArticleLarge = React.forwardRef<HTMLElement, ArticlesProps>(function (props, ref) {
// no full content no render®
if (props.content.length === 0) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import * as React from 'react'
import * as BraveTodayElement from './default'
import CardOptIn from './cards/cardOptIn'
import CardLoading from './cards/cardLoading'
import { ReadFeedItemPayload, DisplayAdViewedPayload, VisitDisplayAdPayload } from '../../../actions/today_actions'
import { PromotedItemViewedPayload, ReadFeedItemPayload, DisplayAdViewedPayload, VisitDisplayAdPayload } from '../../../actions/today_actions'
const Content = React.lazy(() => import('./content'))

export type OnReadFeedItem = (args: ReadFeedItemPayload) => any
export type OnSetPublisherPref = (publisherId: string, enabled: boolean) => any
export type OnPromotedItemViewed = (item: BraveToday.FeedItem) => any
export type OnPromotedItemViewed = (args: PromotedItemViewedPayload) => any
export type OnVisitDisplayAd = (args: VisitDisplayAdPayload) => any
export type OnViewedDisplayAd = (args: DisplayAdViewedPayload) => any
export type GetDisplayAdContent = () => Promise<BraveToday.DisplayAd | null>
Expand Down

0 comments on commit fcee371

Please sign in to comment.