From b13d0bd2acce038fbdf2cbbcb8e15b1a1a275a70 Mon Sep 17 00:00:00 2001 From: Timmy Crawford Date: Wed, 19 Sep 2018 11:23:05 -0700 Subject: [PATCH] Tracks: Record page views on route changes. (https://github.com/woocommerce/woocommerce-admin/pull/452) * Tracks: Record page views on route changes. * adjust regex. --- .../woocommerce-admin/client/layout/index.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/plugins/woocommerce-admin/client/layout/index.js b/plugins/woocommerce-admin/client/layout/index.js index 73edbe2e749..958130dbe64 100644 --- a/plugins/woocommerce-admin/client/layout/index.js +++ b/plugins/woocommerce-admin/client/layout/index.js @@ -6,6 +6,7 @@ import { Component, Fragment } from '@wordpress/element'; import { Router, Route, Switch } from 'react-router-dom'; import { Slot } from 'react-slot-fill'; import PropTypes from 'prop-types'; +import { get } from 'lodash'; /** * Internal dependencies @@ -15,8 +16,43 @@ import Header from 'layout/header'; import { Controller, getPages } from './controller'; import history from 'lib/history'; import Notices from './notices'; +import { recordPageView } from 'lib/tracks'; class Layout extends Component { + componentDidMount() { + this.recordPageViewTrack(); + } + + componentDidUpdate( prevProps ) { + const previousPath = get( prevProps, 'location.pathname' ); + const currentPath = get( this.props, 'location.pathname' ); + + if ( ! previousPath || ! currentPath ) { + return; + } + + if ( previousPath !== currentPath ) { + this.recordPageViewTrack(); + } + } + + recordPageViewTrack() { + const pathname = get( this.props, 'location.pathname' ); + if ( ! pathname ) { + return; + } + + // Remove leading slash, and camel case remaining pathname + let path = pathname.substring( 1 ).replace( /\//g, '_' ); + + // When pathname is `/` we are on the dashboard + if ( path.length === 0 ) { + path = 'dashboard'; + } + + recordPageView( path ); + } + render() { const { isEmbeded, ...restProps } = this.props; return (