UNPKG

3.43 kBMarkdownView Raw
1# `@shopify/react-google-analytics`
2
3[![Build Status](https://travis-ci.org/Shopify/quilt.svg?branch=master)](https://travis-ci.org/Shopify/quilt)
4[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Freact-google-analytics.svg)](https://badge.fury.io/js/%40shopify%2Freact-google-analytics.svg) [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@shopify/react-google-analytics.svg)](https://img.shields.io/bundlephobia/minzip/@shopify/react-google-analytics.svg)
5
6Allows React apps to easily embed tracking pixel iframes
7
8## Installation
9
10```bash
11$ yarn add @shopify/react-google-analytics
12```
13
14## Usage
15
16This library exports a `<Universal />` & a `<GaJS />` component, which allows React apps to easily embed Google Analytics scripts.
17
18## analytics.js example
19
20---
21
22```jsx
23import {Universal} from '@shopify/react-google-analytics';
24
25const UNIVERSAL_GA_ACCOUNT_ID = 'UA-xxxx-xx';
26
27<Universal
28 account={UNIVERSAL_GA_ACCOUNT_ID}
29 domain={shopDomain}
30 disableTracking
31 debug
32 // NOTE: This prop will load and set the debug mode for Google Analytics
33 // https://developers.google.com/analytics/devguides/collection/analyticsjs/debugging
34/>;
35```
36
37#### Set custom variables for analytics.js
38
39```jsx
40import {Universal} from '@shopify/react-google-analytics';
41
42const UNIVERSAL_GA_ACCOUNT_ID = 'UA-xxxx-xx';
43
44<Universal
45 account={UNIVERSAL_GA_ACCOUNT_ID}
46 domain={shopDomain}
47 set={{
48 foo: 'bar', // This translates to ga('set', 'foo', 'bar');
49 }}
50/>;
51```
52
53#### Getting access to the universal tracking instance
54
55```jsx
56import {Universal} from '@shopify/react-google-analytics';
57
58const UNIVERSAL_GA_ACCOUNT_ID = 'UA-xxxx-xx';
59
60<Universal
61 account={UNIVERSAL_GA_ACCOUNT_ID}
62 domain={shopDomain}
63 onLoad={ga => {
64 this.ga = ga;
65 }}
66/>;
67
68<button
69 onClick={() => {
70 this.ga('send', 'event', 'Videos', 'play', 'Cool Video');
71 }}
72>
73 Play Video
74</button>;
75```
76
77For more info on using analytics.js see the [documentation](https://developers.google.com/analytics/devguides/collection/analyticsjs/)
78
79## ga.js example
80
81---
82
83`WARNING: ga.js is a legacy library. If you are starting a new implementation, we recommend you use the latest version of this library, analytics.js.`
84
85```jsx
86import {GaJS} from '@shopify/react-google-analytics';
87
88const GA_JS_ACCOUNT_ID = 'UA-xxxx-xx';
89
90<GaJS
91 account={GA_JS_ACCOUNT_ID}
92 domain={shopDomain}
93 disableTracking
94 // NOTE: Disables the tracking snippet from sending data to Google Analytics.
95 // https://developers.google.com/analytics/devguides/collection/gajs/#disable
96/>;
97```
98
99#### Set custom variables for ga.js
100
101```jsx
102import {GaJS} from '@shopify/react-google-analytics';
103
104const GA_JS_ACCOUNT_ID = 'UA-xxxx-xx';
105
106<GaJS
107 account={GA_JS_ACCOUNT_ID}
108 domain={shopDomain}
109 set={{
110 foo: 'bar', // This translates to _gaq.push(['foo', 'bar']);
111 }}
112/>;
113```
114
115#### Getting access to the ga tracking instance
116
117```jsx
118import {GaJS} from '@shopify/react-google-analytics';
119
120const GA_JS_ACCOUNT_ID = 'UA-xxxx-xx';
121
122<GaJS
123 account={GA_JS_ACCOUNT_ID}
124 domain={shopDomain}
125 onLoad={_gaq => {
126 this._gaq = _gaq;
127 }}
128/>;
129
130<button
131 onClick={() => {
132 this._gaq.push(['_trackEvent', 'button3', 'clicked']);
133 }}
134>
135 Play Video
136</button>;
137```
138
139For more info on using ga.js see the [documentation](https://developers.google.com/analytics/devguides/collection/gajs/)