Skip to content

Commit

Permalink
[docs] Add Google Analytics events (#13451)
Browse files Browse the repository at this point in the history
* added anchorReference prop to Menu

* [docs] Add Google Analytics events
  • Loading branch information
Simon Goldin authored and oliviertassinari committed Oct 30, 2018
1 parent 1990d14 commit f5b38db
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
7 changes: 0 additions & 7 deletions docs/src/config.js

This file was deleted.

34 changes: 30 additions & 4 deletions docs/src/modules/components/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,26 +199,40 @@ class Demo extends React.Component {
render() {
const { classes, demoOptions, githubLocation, index, js: DemoComponent, raw } = this.props;
const { anchorEl, codeOpen } = this.state;
const category = demoOptions.demo;

return (
<div className={classes.root}>
{demoOptions.hideHeader ? null : (
<div>
<div className={classes.header}>
<Tooltip title="See the source on GitHub" placement="top">
<IconButton href={githubLocation} target="_blank" aria-label="GitHub">
<IconButton
ga-event-category={category}
ga-event-action="github"
href={githubLocation}
target="_blank"
aria-label="GitHub"
>
<Github />
</IconButton>
</Tooltip>
{demoOptions.hideEditButton ? null : (
<Tooltip title="Edit in CodeSandbox" placement="top">
<IconButton onClick={this.handleClickCodeSandbox} aria-label="CodeSandbox">
<IconButton
ga-event-category={category}
ga-event-action="codesandbox"
onClick={this.handleClickCodeSandbox}
aria-label="CodeSandbox"
>
<EditIcon />
</IconButton>
</Tooltip>
)}
<Tooltip title={codeOpen ? 'Hide the source' : 'Show the source'} placement="top">
<IconButton
ga-event-category={category}
ga-event-action="expand"
onClick={this.handleClickCodeOpen}
aria-label={`Source of demo n°${index}`}
>
Expand Down Expand Up @@ -248,9 +262,21 @@ class Demo extends React.Component {
horizontal: 'right',
}}
>
<MenuItem onClick={this.handleClickCopy}>Copy the source</MenuItem>
<MenuItem
ga-event-category={category}
ga-event-action="copy"
onClick={this.handleClickCopy}
>
Copy the source
</MenuItem>
{demoOptions.hideEditButton ? null : (
<MenuItem onClick={this.handleClickStackBlitz}>Edit in StackBlitz</MenuItem>
<MenuItem
ga-event-category={category}
ga-event-action="stackblitz"
onClick={this.handleClickStackBlitz}
>
Edit in StackBlitz
</MenuItem>
)}
</Menu>
</div>
Expand Down
57 changes: 46 additions & 11 deletions docs/src/modules/components/GoogleAnalytics.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
import React from 'react';

class GoogleAnalytics extends React.Component {
componentDidMount() {
if (process.env.NODE_ENV !== 'production') {
return;
function handleClick(event) {
const rootNode = document;
let element = event.target;

while (element && element !== rootNode) {
const category = element.getAttribute('ga-event-category');

// We reach a tracking element, no need to look higher in the dom tree.
if (category) {
window.ga('send', {
hitType: 'event',
eventCategory: category,
eventAction: element.getAttribute('ga-event-action'),
});
break;
}

element = element.parentNode;
}
}

let binded = false;

// So we can write code like:
//
// <Button
// ga-event-category="demo"
// ga-event-action="expand"
// >
// Foo
// </Button>
function bindEvents() {
if (binded) {
return;
}

binded = true;
document.addEventListener('click', handleClick);
}

class GoogleAnalytics extends React.Component {
googleTimer = null;

componentDidMount() {
bindEvents();
// Wait for the title to be updated.
this.timer = setTimeout(() => {
window.ga('set', {
setTimeout(() => {
window.ga('send', {
hitType: 'pageview',
page: window.location.pathname,
});
window.ga('send', { hitType: 'pageview' });
});
}

componentWillUnmount() {
clearTimeout(this.timer);
}

render() {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions pages/_document.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import Document, { Head, Main, NextScript } from 'next/document';
import getPageContext from 'docs/src/modules/styles/getPageContext';
import config from 'docs/src/config';

// You can find a benchmark of the available CSS minifiers under
// https://github.com/GoalSmashers/css-minification-benchmark
Expand All @@ -21,6 +20,8 @@ if (process.env.NODE_ENV === 'production') {
cleanCSS = new CleanCSS();
}

const GOOGLE_ID = process.env.NODE_ENV === 'production' ? 'UA-106598593-2' : 'UA-106598593-3';

class MyDocument extends Document {
render() {
const { canonical, pageContext } = this.props;
Expand Down Expand Up @@ -64,7 +65,7 @@ class MyDocument extends Document {
dangerouslySetInnerHTML={{
__html: `
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', '${config.google.id}', 'material-ui.com');
window.ga('create', '${GOOGLE_ID}', 'auto');
`,
}}
/>
Expand Down

0 comments on commit f5b38db

Please sign in to comment.