React-snap: Not working correctly with react-ga

Created on 25 Oct 2018  路  2Comments  路  Source: stereobooster/react-snap

I am trying to use the react-snap and react-ga packages in my project, but no analytics are being gathered after I "snap" my pages. React-ga works great if I don't use react-snap, but as soon as I build the project using react-snap no data is being sent at all.

I see direct references to react-ga in the readme and recipe files, so it seems like these two should work together. Is there some obvious piece that I'm missing? Other than this issue, everything else appears to work really well with react-snap.

The analytics I'm trying to gather are minimal. I'm gathering my analytics in the componentsWillMount() method and in various onClick methods on my site. I've tried setting the "skipThirdPartyRequests" flag in my packages.json file, but that didn't fix the issue.

Is there something I'm missing to make react-snap play nicely with react-ga?

question

Most helpful comment

I'm able to use react-snap with react-ga. Try wrapping react-ga with a function that returns a stub when reach-snap is running.

const host = window.location.hostname;
const isLocalHost = host === 'localhost';
const isSnap = navigator.userAgent === 'ReactSnap';

const gaOn = !isLocalHost && !isSnap;

if (gaOn) {
    ReactGA.initialize(process.env.REACT_APP_GA_SITE_CODE);
}

export function ga() {
    if (gaOn) {
        return ReactGA;
    } else { // Return stub
        return {
            set: () => { },
            event: () => { },
            pageview: () => { }
        }
    }
}

All 2 comments

I'm able to use react-snap with react-ga. Try wrapping react-ga with a function that returns a stub when reach-snap is running.

const host = window.location.hostname;
const isLocalHost = host === 'localhost';
const isSnap = navigator.userAgent === 'ReactSnap';

const gaOn = !isLocalHost && !isSnap;

if (gaOn) {
    ReactGA.initialize(process.env.REACT_APP_GA_SITE_CODE);
}

export function ga() {
    if (gaOn) {
        return ReactGA;
    } else { // Return stub
        return {
            set: () => { },
            event: () => { },
            pageview: () => { }
        }
    }
}

I guess, question is answered. If not feel free to reopen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

raRaRa picture raRaRa  路  8Comments

aheissenberger picture aheissenberger  路  7Comments

rohan-deshpande picture rohan-deshpande  路  5Comments

kaloraat picture kaloraat  路  5Comments

wInevitable picture wInevitable  路  8Comments