Skip to content

Commit

Permalink
remove correlation between codefund/carbon and fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Dec 23, 2019
1 parent d434dbb commit b3ec703
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 35 deletions.
50 changes: 45 additions & 5 deletions docs/src/modules/components/Ad.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ const inHouseAds = [

function Ad(props) {
const { classes } = props;
const { current: random } = React.useRef(Math.random());
const t = useSelector(state => state.options.t);

const timerAdblock = React.useRef();
Expand Down Expand Up @@ -161,24 +160,65 @@ function Ad(props) {
if (adblock) {
minHeight = 'auto';

if (Math.random() >= 0.8) {
if (Math.random() < 0.2) {
children = getAdblock(classes, t);
} else {
children = <AdInHouse ad={inHouseAds[Math.floor(inHouseAds.length * random)]} />;
children = <AdInHouse ad={inHouseAds[Math.floor(inHouseAds.length * Math.random())]} />;
}
}

if (!children) {
if (carbonOut || codeFundOut) {
children = <AdInHouse ad={inHouseAds[Math.floor(inHouseAds.length * random)]} />;
children = <AdInHouse ad={inHouseAds[Math.floor(inHouseAds.length * Math.random())]} />;
minHeight = 'auto';
} else if (random >= 0.65) {
} else if (Math.random() < 0.35) {
children = <AdCodeFund />;
} else {
children = <AdCarbon />;
}
}

React.useEffect(() => {
// Avoid a flood of events.
if (Math.random < 0.1) {
return null;
}

const delay = setTimeout(() => {
let type;

if (children.type === AdCodeFund) {
type = 'codefund';
} else if (children.type === AdCarbon) {
type = 'carbon';
} else if (children.type === AdInHouse) {
type = 'in-house';
} else {
type = 'disable-adblock';
}

window.ga('send', {
hitType: 'event',
eventCategory: 'ad',
eventAction: 'display',
eventLabel: type,
});

if (type === 'in-house') {
window.ga('send', {
hitType: 'event',
eventCategory: 'in-house-ad',
eventAction: 'display',
eventLabel: children.props.ad.name,
});
}
}, 2000);

return () => {
clearTimeout(delay);
};
}, [children.type, children.props.ad]);

return (
<span className={classes.root} style={{ minHeight }}>
{children}
Expand Down
1 change: 1 addition & 0 deletions docs/src/modules/components/AdInHouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const useStyles = makeStyles(theme => ({
export default function AdInHouse(props) {
const { ad } = props;
const classes = useStyles();

/* eslint-disable material-ui/no-hardcoded-labels, react/no-danger */
return (
<span className={classes.root}>
Expand Down
54 changes: 30 additions & 24 deletions docs/src/modules/components/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ function Demo(props) {

const showSourceHint = demoHovered && !sourceHintSeen;
const DemoComponent = demoData.Component;
const gaCategory = demoOptions.demo;
const demoName = getDemoName(demoData.githubLocation);
const demoSandboxedStyle = React.useMemo(
() => ({
Expand Down Expand Up @@ -339,7 +338,7 @@ function Demo(props) {
demo={demo}
codeOpen={codeOpen}
codeVariant={codeVariant}
gaEventCategory={gaCategory}
gaEventLabel={demoOptions.demo}
onLanguageClick={handleCodeLanguageClick}
/>
<div className={classes.headerButtons}>
Expand All @@ -353,30 +352,15 @@ function Demo(props) {
>
<IconButton
aria-label={showCodeLabel}
data-ga-event-category={gaCategory}
data-ga-event-category="demo"
data-ga-event-label={demoOptions.demo}
data-ga-event-action="expand"
onClick={handleClickCodeOpen}
color={demoHovered ? 'primary' : 'default'}
>
<CodeIcon fontSize="small" />
</IconButton>
</Tooltip>
<Tooltip
classes={{ popper: classes.tooltip }}
title={t('viewGitHub')}
placement="top"
>
<IconButton
aria-label={t('viewGitHub')}
data-ga-event-category={gaCategory}
data-ga-event-action="github"
href={demoData.githubLocation}
target="_blank"
rel="noopener nofollow"
>
<GitHubIcon fontSize="small" />
</IconButton>
</Tooltip>
{demoOptions.hideEditButton ? null : (
<Tooltip
classes={{ popper: classes.tooltip }}
Expand All @@ -385,14 +369,32 @@ function Demo(props) {
>
<IconButton
aria-label={t('codesandbox')}
data-ga-event-category={gaCategory}
data-ga-event-category="demo"
data-ga-event-label={demoOptions.demo}
data-ga-event-action="codesandbox"
onClick={handleClickCodeSandbox}
>
<EditIcon fontSize="small" />
</IconButton>
</Tooltip>
)}
<Tooltip
classes={{ popper: classes.tooltip }}
title={t('viewGitHub')}
placement="top"
>
<IconButton
aria-label={t('viewGitHub')}
data-ga-event-category="demo"
data-ga-event-label={demoOptions.demo}
data-ga-event-action="github"
href={demoData.githubLocation}
target="_blank"
rel="noopener nofollow"
>
<GitHubIcon fontSize="small" />
</IconButton>
</Tooltip>
<IconButton
onClick={handleClickMore}
aria-owns={anchorEl ? 'demo-menu-more' : undefined}
Expand All @@ -417,30 +419,34 @@ function Demo(props) {
}}
>
<MenuItem
data-ga-event-category={gaCategory}
data-ga-event-category="demo"
data-ga-event-label={demoOptions.demo}
data-ga-event-action="copy"
onClick={handleClickCopy}
>
{t('copySource')}
</MenuItem>
{demoOptions.hideEditButton ? null : (
<MenuItem
data-ga-event-category={gaCategory}
data-ga-event-category="demo"
data-ga-event-label={demoOptions.demo}
data-ga-event-action="stackblitz"
onClick={handleClickStackBlitz}
>
{t('stackblitz')}
</MenuItem>
)}
<MenuItem
data-ga-event-category={gaCategory}
data-ga-event-category="demo"
data-ga-event-label={demoOptions.demo}
data-ga-event-action="copy-js-source-link"
onClick={createHandleCodeSourceLink(`${demoName}.js`)}
>
{t('copySourceLinkJS')}
</MenuItem>
<MenuItem
data-ga-event-category={gaCategory}
data-ga-event-category="demo"
data-ga-event-label={demoOptions.demo}
data-ga-event-action="copy-ts-source-link"
onClick={createHandleCodeSourceLink(`${demoName}.tsx`)}
>
Expand Down
10 changes: 6 additions & 4 deletions docs/src/modules/components/DemoLanguages.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const styles = {
};

function DemoLanguages(props) {
const { classes, codeOpen, codeVariant, demo, gaEventCategory, onLanguageClick } = props;
const { classes, codeOpen, codeVariant, demo, gaEventLabel, onLanguageClick } = props;
const hasTSVariant = demo.rawTS;
const t = useSelector(state => state.options.t);

Expand All @@ -41,8 +41,9 @@ function DemoLanguages(props) {
className={classes.toggleButton}
value={CODE_VARIANTS.JS}
aria-label={t('showJSSource')}
data-ga-event-category={gaEventCategory}
data-ga-event-category="demo"
data-ga-event-action="source-js"
data-ga-event-label={gaEventLabel}
>
<JavaScriptIcon />
</ToggleButton>
Expand All @@ -51,8 +52,9 @@ function DemoLanguages(props) {
value={CODE_VARIANTS.TS}
disabled={!hasTSVariant}
aria-label={t('showTSSource')}
data-ga-event-category={gaEventCategory}
data-ga-event-category="demo"
data-ga-event-action="source-ts"
data-ga-event-label={gaEventLabel}
>
<TypeScriptIcon />
</ToggleButton>
Expand All @@ -66,7 +68,7 @@ DemoLanguages.propTypes = {
codeOpen: PropTypes.bool.isRequired,
codeVariant: PropTypes.string.isRequired,
demo: PropTypes.object.isRequired,
gaEventCategory: PropTypes.string.isRequired,
gaEventLabel: PropTypes.string.isRequired,
onLanguageClick: PropTypes.func,
};

Expand Down
3 changes: 1 addition & 2 deletions docs/src/modules/components/GoogleAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';
// Foo
// </Button>
function handleClick(event) {
const rootNode = document;
let element = event.target;

while (element && element !== rootNode) {
while (element && element !== document) {
const category = element.getAttribute('data-ga-event-category');

// We reach a tracking element, no need to look higher in the dom tree.
Expand Down

0 comments on commit b3ec703

Please sign in to comment.