Google analytics provides SUSI.AI Admins with a way to analyze traffic and get advanced metrics. Google Analytics first collects data, computes the data, and showcases it on console dashboard. It is used for keeping track of user behavior on the website.
How Google Analytics Work
Below shown are fields used by Google Analytics to get user data. A cookie is stored into user browser. _ga stays in the browser for 2 years, _gid for 2 days.
Whenever a user performs an event like a mouse click, page change, open popup, add query strings to URL, information is sent to Google Analytics using an API call describing the user event. Below is the photo describing it:
The above-bordered boxes consist of information sent by google analytics. The information consists of:
- The user identification code
- The device resolution of screen used by a user
- User language
- The URL of the page user is on
How it processes data
When a user with tracking code lands on SUSI.AI, Google Analytics creates a unique random identity and attaches it to the user cookie.
Each new user is given a unique ID. Whenever a new ID is detected, analytics considers it as a new user. But when an existing ID is detected, it’s considered as a returning user and set over with the hit.
A unique ID code is fetched from every new unique user. Whenever a new user ID is detected in the call, Google Analytics treats the unique ID as a new user. If the ID matches from earlier ID, the user is a returning user and calculates the metrics another way. Each new user gets a unique ID.
However, a new user is detected if the same user clears out the browser cookie or uses another device over the same IP address to view the webpage.
When an existing ID is detected, it’s considered as a returning user and set over with the hit.
Code Integration
Google Analytics must be initialized using initialize(gaTrackingID) function before any of the other tracking functions will record any data. Using react-ga ga(‘create’, …), the values are sent to google analytics
import withTracker from './withTracker'; import GoogleAnalytics from 'react-ga'; .. actions.getApiKeys().then(({ payload }) => { const { keys: { googleAnalyticsKey = null }, } = payload; googleAnalyticsKey && GoogleAnalytics.initialize(googleAnalyticsKey); }); .. <Route exact path="/" component={withTracker(BrowseSkill)} /> <Route exact path="/chat" component={withTracker(ChatApp)} />
Higher Order Component for Tracking page activity
A Higher Order Component is a function that returns an enhanced component by adding some more properties or logic and allows reusing component logic.
Using HOC for tracking page helped by not exposing internal component with tracking. The withTracker HOC wraps all the component, which SUSI.AI wants to track and exposes a trackerPage method.
- Whenever a component mounts, we track the new pages using componentDidMount.
- Whenever the component updates and location of browser url changes, we track the sub pages using componentDidUpdate lifecycle hook.
import React, { Component } from 'react'; import GoogleAnalytics from 'react-ga'; import PropTypes from 'prop-types'; const withTracker = (WrappedComponent, options = { }) => { const trackPage = page => { GoogleAnalytics.set({ page, ...options, }); GoogleAnalytics.pageview(page); }; const HOC = class extends Component { componentDidMount() { window.scrollTo(0, 0); const page = this.props.location.pathname + this.props.location.search; trackPage(page); } componentDidUpdate(prevProps) { const currentPage = prevProps.location.pathname + prevProps.location.search; const nextPage = this.props.location.pathname + this.props.location.search; if (currentPage !== nextPage) { trackPage(nextPage); } if (this.props.location.pathname !== prevProps.location.pathname) { window.scrollTo(0, 0); } } render() { return <WrappedComponent {...this.props} />; } }; HOC.propTypes = { location: PropTypes.object, }; return HOC; }; export default withTracker;
The Higher-Order Component pattern turned out to be really useful to achieve D.R.Y (Don’t Repeat Yourself) and keeping component separate from tracking. With React Analytics being added, we can track various metrics, live users on site and see how SUSI.AI traffic is performing over time.
Resources
- Link to PR: https://github.com/fossasia/susi.ai/pull/2634
- React-ga documentation: https://github.com/react-ga/react-ga
- Google analytics: https://analytics.google.com/analytics/web/
- Higher-Order Components Introduction: https://reactjs.org/docs/higher-order-components.html
Tags
SUSI.AI, FOSSASIA, GSoC19, Google Analytics, Higher Order Components