This blog here recommends using react-ga and just adding react events every, eg on event clicks, like:

export default class SomeComponent extends React.Component {

    handleClick() {
        ReactGA.event({
            category: 'Navigation',
            action: 'Clicked Link',
        });
    }

    render() {

        return (
            <div>
                <a onClick={()=>{this.handleClick()}}>Link</a>
            </div>
        );
    }
}

Is this the best way to add google analytics?

It's straightforward - but I hate the idea of cludging up all my code with that kind of boiler plate.

I would prefer an entirely separate area to declare my the events I want to track, and define them by element id or something like that.

  • This blog here gives a hint a better solution - use middleware on your redux actions - which might work for a lot of cases - but not for, say, a drop down menu: medium.com/@GildasQ/… – dwjohnston Nov 8 at 0:53

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.