Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

add google analytics to react

Bash/CMD: yarn add react-ga

//useAnalyticsEventTracker.jsx

import React from "react";
import ReactGA from "react-ga";

const AnalyticsEventTracker = (category="Blog category") => {
  const eventTracker = (action = "test action", label = "test label") => {
    ReactGA.event({category, action, label});
  }
  return eventTracker;
}
export default AnalyticsEventTracker;

//ContactUs.jsx
import AnalyticsEventTracker from './AnalyticsEventTracker';

const ContactUs = () => {
  const gaEventTracker = AnalyticsEventTracker('Contact us');
  return(
  <div>
    <h3>Contact Us</h3>
     <div> 
       <a href="#" onClick={()=>gaEventTracker('call')}>Call Us</a>
      </div>
     <div>
       <a href="mailto:someone@example.com" onClick={()=>gaEventTracker('email')}>Write to us</a>
      </div>
  </div>)
};
Source by blog.saeloun.com #
 
PREVIOUS NEXT
Tagged: #add #google #analytics #react
ADD COMMENT
Topic
Name
2+9 =