From ce4010e52b38eef65be1de050130fb83a8d0eb09 Mon Sep 17 00:00:00 2001 From: Surma Date: Tue, 6 Nov 2018 12:13:02 +0000 Subject: [PATCH] Add analytics script (fixes #174) --- src/components/results/index.tsx | 11 +++++++++++ src/index.ts | 9 +++++++++ src/missing-types.d.ts | 9 +++++++++ 3 files changed, 29 insertions(+) diff --git a/src/components/results/index.tsx b/src/components/results/index.tsx index 9d4a6c0c..acebc26a 100644 --- a/src/components/results/index.tsx +++ b/src/components/results/index.tsx @@ -58,6 +58,16 @@ export default class Results extends Component { this.props.onCopyToOtherClick(); } + @bind + onDownload() { + ga('send', 'event', 'compression', 'download', { + // GA can’t do floats. So we round to ints. + metric1: Math.floor(this.props.source!.file.size), + metric2: Math.floor(this.props.imageFile!.size), + metric3: Math.floor(this.props.imageFile!.size / this.props.source!.file.size * 1000), + }); + } + render( { source, imageFile, downloadUrl, children, copyDirection, buttonPosition }: Props, { showLoadingState }: State, @@ -93,6 +103,7 @@ export default class Results extends Component { href={downloadUrl} download={imageFile.name} title="Download" + onClick={this.onDownload} > diff --git a/src/index.ts b/src/index.ts index b88db26b..a356847c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,3 +12,12 @@ if (!('customElements' in self)) { } else { init(); } + +window.ga = window.ga || ((...args) => (ga.q = ga.q || []).push(args)); +ga('create', 'UA-128752250-1', 'auto'); +ga('set', 'transport', 'beacon'); +ga('send', 'pageview'); + // Load the GA script +const s = document.createElement('script'); +s.src = 'https://www.google-analytics.com/analytics.js'; +document.head!.appendChild(s); diff --git a/src/missing-types.d.ts b/src/missing-types.d.ts index 7f18e87e..6535ccf0 100644 --- a/src/missing-types.d.ts +++ b/src/missing-types.d.ts @@ -34,3 +34,12 @@ declare module 'url-loader!*' { } declare var VERSION: string; + +declare var ga: { + (...args: any[]): void; + q: any[]; +}; + +interface Window { + ga: typeof ga; +}