From 5917b872c210e9305cce1783094a4ea491ebc949 Mon Sep 17 00:00:00 2001 From: Chuck Dries Date: Sun, 20 Jun 2021 03:19:30 -0700 Subject: [PATCH] only attempt to send analytics events in prod --- src/pages/index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/pages/index.js b/src/pages/index.js index 8247109..b38e1e9 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -11,6 +11,8 @@ import { HeroA } from '../components/IndexComponents'; // TODO: better text colors in situations of low contrast +const env = process.env.GATSBY_ACTIVE_ENV || process.env.NODE_ENV || 'development'; + const getDifferentRand = (range, lastNs, iterations = 0) => { const n = Math.floor(Math.random() * range); if (lastNs.findIndex(x => x === n) > -1 && iterations < 5) { @@ -31,12 +33,11 @@ const IndexPage = ({ data: { allFile: { edges } } }) => { const shuffleImage = React.useCallback((currentImage) => { const lastThreeImages = JSON.parse(localStorage.getItem('lastHeros')) || []; - try { - // eslint-disable-next-line - posthog.capture('[shuffle image]', { currentImage: currentImage?.base }); - } catch (e) { - console.log('failed to send shuffle image', e); - // do nothing + if (env === 'production') { + try { + // eslint-disable-next-line + posthog.capture('[shuffle image]', { currentImage: currentImage?.base }); + } catch (e) {/* do nothing */} } const index = getDifferentRand(images.length, lastThreeImages); localStorage.setItem('lastHeros', JSON.stringify(take(3, [index, ...lastThreeImages])));