From 4cc7338534d4c66442036a2ade8aefdc9baa3803 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 10 Feb 2018 23:23:50 -0800 Subject: [PATCH] Analytics (#582) * gtm > ga * Record pageviews in SPA * Remove home-rolled tracking in favor of autotrack * cleanup --- .../{ScrollToTop => }/ScrollToTop.js | 0 app/components/ScrollToTop/index.js | 3 - app/index.js | 20 ++- app/scenes/Document/Document.js | 1 + package.json | 3 +- shared/components/Analytics.js | 16 +-- webpack.config.js | 2 +- yarn.lock | 131 ++++++++++++++++-- 8 files changed, 148 insertions(+), 28 deletions(-) rename app/components/{ScrollToTop => }/ScrollToTop.js (100%) delete mode 100644 app/components/ScrollToTop/index.js diff --git a/app/components/ScrollToTop/ScrollToTop.js b/app/components/ScrollToTop.js similarity index 100% rename from app/components/ScrollToTop/ScrollToTop.js rename to app/components/ScrollToTop.js diff --git a/app/components/ScrollToTop/index.js b/app/components/ScrollToTop/index.js deleted file mode 100644 index 0f8823a1..00000000 --- a/app/components/ScrollToTop/index.js +++ /dev/null @@ -1,3 +0,0 @@ -// @flow -import ScrollToTop from './ScrollToTop'; -export default ScrollToTop; diff --git a/app/index.js b/app/index.js index d9e6b54f..b772f37d 100644 --- a/app/index.js +++ b/app/index.js @@ -1,5 +1,5 @@ // @flow -import React from 'react'; +import * as React from 'react'; import { render } from 'react-dom'; import { Provider } from 'mobx-react'; import { @@ -49,7 +49,7 @@ const RedirectDocument = ({ match }: { match: Object }) => ( globalStyles(); render( -
+ @@ -120,6 +120,20 @@ render( {DevTools && } -
, + , document.getElementById('root') ); + +window.addEventListener('load', async () => { + // installation does not use Google Analytics, or tracking is blocked on client + // no point loading the rest of the analytics bundles + if (!process.env.GOOGLE_ANALYTICS_ID || !window.ga) return; + + await import('autotrack/lib/plugins/outbound-link-tracker'); + await import('autotrack/lib/plugins/url-change-tracker'); + + window.ga('create', process.env.GOOGLE_ANALYTICS_ID, 'auto'); + window.ga('require', 'outboundLinkTracker'); + window.ga('require', 'urlChangeTracker'); + window.ga('send', 'pageview'); +}); diff --git a/app/scenes/Document/Document.js b/app/scenes/Document/Document.js index 155eb34f..8539561a 100644 --- a/app/scenes/Document/Document.js +++ b/app/scenes/Document/Document.js @@ -113,6 +113,7 @@ class DocumentScene extends Component { if (document) { this.props.ui.setActiveDocument(document); + // Cache data if user enters edit mode and cancels this.editCache = document.text; if (!this.isEditing) document.view(); diff --git a/package.json b/package.json index e3085c97..d2345b0f 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "@tommoor/slate-drop-or-paste-images": "^0.8.1", "@tommoor/slate-edit-code": "^0.13.3", "aws-sdk": "^2.135.0", + "autotrack": "^2.4.1", "babel-core": "^6.24.1", "babel-eslint": "^8.1.2", "babel-loader": "^7.1.2", @@ -145,7 +146,7 @@ "query-string": "^4.3.4", "randomstring": "1.1.5", "raw-loader": "^0.5.1", - "react": "^16.1.0", + "react": "^16.2.0", "react-avatar-editor": "^10.3.0", "react-dom": "^16.1.0", "react-dropzone": "4.2.1", diff --git a/shared/components/Analytics.js b/shared/components/Analytics.js index 0e37197c..6524c339 100644 --- a/shared/components/Analytics.js +++ b/shared/components/Analytics.js @@ -1,24 +1,20 @@ // @flow -import React from 'react'; +import * as React from 'react'; function Analytics() { - const id = process.env.GOOGLE_ANALYTICS_ID; - if (!id) return null; + if (!process.env.GOOGLE_ANALYTICS_ID) return null; return ( - -