Skip to content

Commit

Permalink
Added new moat tracking component to react gif component
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksantan committed May 2, 2024
1 parent c6ba8ed commit 92415ff
Showing 1 changed file with 3 additions and 56 deletions.
59 changes: 3 additions & 56 deletions packages/react-components/src/components/gif.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
'use client'
import { giphyBlue, giphyGreen, giphyPurple, giphyRed, giphyYellow } from '@giphy/colors'
import { IGif, IUser, ImageAllTypes } from '@giphy/js-types'
import {
Logger,
getAltText,
getBestRendition,
getGifHeight,
constructMoatData,
injectTrackingPixel,
} from '@giphy/js-util'
import { Logger, getAltText, getBestRendition, getGifHeight } from '@giphy/js-util'
import MoatTracking from './moat-tracking'
import 'intersection-observer'
import moat from '@giphy/moat-loader'
import React, { ElementType, ReactNode, SyntheticEvent, useContext, useEffect, useRef, useState } from 'react'
import styled from 'styled-components'
import * as pingback from '../util/pingback'
Expand All @@ -19,8 +12,6 @@ import VerifiedBadge from './attribution/verified-badge'
import { PingbackContext } from './pingback-context-manager'
import { GifOverlayProps } from './types'

const moatLoader = moat.loadMoatTag('giphydisplay879451385633', 'https://giphyscripts.s3.amazonaws.com/moat/moatad.js')

const Container = styled.div`
position: relative;
display: block;
Expand Down Expand Up @@ -60,11 +51,6 @@ export type EventProps = {
onGifKeyPress?: (gif: IGif, e: SyntheticEvent<HTMLElement, Event>) => void
}

function useMutableRef<T>(initialValue?: T) {
const [ref] = useState<{ current: T | undefined }>({ current: initialValue })
return ref
}

type GifProps = {
gif: IGif
width: number
Expand Down Expand Up @@ -144,8 +130,6 @@ const Gif = ({
const hoverTimeout = useRef<number>()
// fire onseen ref (changes per gif, so need a ref)
const sendOnSeen = useRef<(_: IntersectionObserverEntry) => void>(noop)
// moat ad number
const moatAdNumber = useMutableRef<number>()
// are we displaying an ad
const isAd = Object.keys(bottleData).length > 0
// custom pingback
Expand Down Expand Up @@ -212,28 +196,8 @@ const Gif = ({
}
}

const trackWithMoat = async () => {
if (shouldShowMedia && container.current) {
const { bottle_data: bottleData, response_id } = gif
const moatCompatibleData = constructMoatData(bottleData as any)
if (moatCompatibleData) {
moatCompatibleData.zMoatSession = response_id
await moatLoader
if (container.current) {
moatAdNumber.current = moat.startTracking(container.current, moatCompatibleData)
}
}
}
}

const onImageLoad = (e: SyntheticEvent<HTMLElement, Event>) => {
watchGif()
if (isAd) {
if (moatAdNumber.current === undefined) {
trackWithMoat()
}
injectTrackingPixel(bottleData.tags)
}
onGifVisible(gif, e) // gif is visible, perhaps just partially
setLoadedClassName(Gif.imgLoadedClassName)
}
Expand All @@ -250,23 +214,6 @@ const Gif = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [gif.id])

const stopTracking = () => {
// if we have a moat ad number
if (moatAdNumber.current !== undefined) {
// stop tracking
moat.stopTracking(moatAdNumber.current)
// remove the moat ad number
moatAdNumber.current = undefined
}
}

// if this component goes from showing an ad to not an ad
useEffect(() => {
if (!shouldShowMedia) {
return () => stopTracking()
}
}, [shouldShowMedia])

useEffect(() => {
showGifObserver.current = new IntersectionObserver(([entry]: IntersectionObserverEntry[]) => {
const { isIntersecting } = entry
Expand All @@ -283,7 +230,6 @@ const Gif = ({
if (showGifObserver.current) showGifObserver.current.disconnect()
if (fullGifObserver.current) fullGifObserver.current.disconnect()
if (hoverTimeout.current) clearTimeout(hoverTimeout.current)
stopTracking()
}
}, [])
const height = forcedHeight || getGifHeight(gif, width)
Expand Down Expand Up @@ -345,6 +291,7 @@ const Gif = ({
onLoad={shouldShowMedia ? onImageLoad : () => {}}
/>
</picture>
{isAd && <MoatTracking bottleData={bottleData} />}
{Overlay && (
// only render the overlay on the client since it depends on shouldShowMedia
<RenderOnClient>
Expand Down

0 comments on commit 92415ff

Please sign in to comment.