Skip to content

Commit

Permalink
[docs-infra] Fix analytics about inline ads (#41474) (#41819)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored Apr 9, 2024
1 parent c102ebb commit 16d523f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/src/modules/components/Ad.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Box from '@mui/material/Box';
import Paper from '@mui/material/Paper';
import AdCarbon from 'docs/src/modules/components/AdCarbon';
import AdInHouse from 'docs/src/modules/components/AdInHouse';
import { GA_ADS_DISPLAY_RATIO } from 'docs/src/modules/constants';
import { AdContext, adShape } from 'docs/src/modules/components/AdManager';
import { useTranslate } from '@mui/docs/i18n';

Expand Down Expand Up @@ -197,7 +198,7 @@ export default function Ad() {

React.useEffect(() => {
// Avoid an exceed on the Google Analytics quotas.
if (Math.random() < 0.9 || !eventLabel) {
if (Math.random() > GA_ADS_DISPLAY_RATIO || !eventLabel) {
return undefined;
}

Expand Down
1 change: 1 addition & 0 deletions docs/src/modules/components/AdCarbon.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export function AdCarbonInline(props) {
name: ad.company,
description: `<strong>${ad.company}</strong> - ${ad.description}`,
poweredby: 'Carbon',
label: 'carbon-demo-inline',
}}
/>
</React.Fragment>
Expand Down
13 changes: 13 additions & 0 deletions docs/src/modules/components/AdDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
import { adShape } from 'docs/src/modules/components/AdManager';
import { GA_ADS_DISPLAY_RATIO } from 'docs/src/modules/constants';
import { adStylesObject } from 'docs/src/modules/components/ad.styles';

const Root = styled('span', { shouldForwardProp: (prop) => prop !== 'shape' })(({
Expand All @@ -23,6 +24,18 @@ const Root = styled('span', { shouldForwardProp: (prop) => prop !== 'shape' })((
export default function AdDisplay(props) {
const { ad, className, shape = 'auto' } = props;

React.useEffect(() => {
// Avoid an exceed on the Google Analytics quotas.
if (Math.random() > GA_ADS_DISPLAY_RATIO || !ad.label) {
return;
}

window.gtag('event', 'ad', {
eventAction: 'display',
eventLabel: ad.label,
});
}, [ad.label]);

/* eslint-disable material-ui/no-hardcoded-labels, react/no-danger */
return (
<Root shape={shape === 'inline' ? 'inline' : adShape} className={className}>
Expand Down
4 changes: 4 additions & 0 deletions docs/src/modules/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ const LANGUAGES_LABEL = [
},
];

// The ratio of ads display sending event to Google Analytics
const GA_ADS_DISPLAY_RATIO = 0.1;

module.exports = {
CODE_VARIANTS,
LANGUAGES_LABEL,
CODE_STYLING,
GA_ADS_DISPLAY_RATIO,
};

0 comments on commit 16d523f

Please sign in to comment.