Skip to content

Commit decc071

Browse files
committed
Release 0.1.17
1 parent c639ef0 commit decc071

File tree

4 files changed

+79
-28
lines changed

4 files changed

+79
-28
lines changed

README.md

+40-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ npm install react-g-analytics
1212

1313
## Features
1414

15-
* Automatically load google analytics scripts (React component)
15+
* Automatically load google analytics scripts (optional - id parameter)
1616
* Automatically send pageview when user will change current route of react-router
1717

1818

1919
## Usage
2020

2121
### App.jsx
2222

23-
Application part (load google analytics script to your webpage on the client side).
23+
Application part (load google analytics script to your webpage on the client side).
2424
ReactGAnalytics has parameter ID (use your own ID)
2525

2626
```js
@@ -61,7 +61,7 @@ var routes = module.exports = (
6161

6262
### client.js
6363

64-
Here is a simple client side
64+
Here is a simple client side
6565

6666
```js
6767
var React = require('react');
@@ -76,7 +76,43 @@ router.run(function(Handler, state) {
7676
React.render(React.createElement(Handler, {}), node);
7777
});
7878
```
79-
79+
80+
## Set
81+
82+
If you want to set google analytics parameters after load you can use property named set. Here is small example:
83+
84+
```js
85+
var React = require('react');
86+
var GoogleAnalytics = require('react-g-analytics');
87+
var RouteHandler = require('react-router').RouteHandler;
88+
89+
var set = {
90+
anonymizeIp: true
91+
};
92+
93+
var App = module.exports = React.createClass({
94+
render: function() {
95+
return (
96+
<div id="application">
97+
<GoogleAnalytics id="UA-*******-**" set={set} />
98+
<RouteHandler />
99+
</div>
100+
);
101+
}
102+
});
103+
```
104+
105+
## Skip loading google analytics scripts
106+
107+
If you are loading the GA in different way. You can skip autoload of the GA script simply:
108+
Do not enter your google analytics ID as parameter.
109+
110+
## Try our other React components
111+
112+
- Translate your great project [react-translate-maker](https://github.com/CherrySoftware/react-translate-maker)
113+
- Google AdSense via Google Publisher Tag [react-google-publisher-tag](https://github.com/seeden/react-google-publisher-tag)
114+
115+
80116
## Credits
81117

82118
[Zlatko Fedor](https://github.com/seeden)

dist/index.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Object.defineProperty(exports, '__esModule', {
66

77
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
88

9-
var _get = function get(_x3, _x4, _x5) { var _again = true; _function: while (_again) { var object = _x3, property = _x4, receiver = _x5; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x3 = parent; _x4 = property; _x5 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
9+
var _get = function get(_x4, _x5, _x6) { var _again = true; _function: while (_again) { var object = _x4, property = _x5, receiver = _x6; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x4 = parent; _x5 = property; _x6 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
1010

1111
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
1212

@@ -18,13 +18,15 @@ var _react = require('react');
1818

1919
var _react2 = _interopRequireDefault(_react);
2020

21+
var _lodashObjectKeys = require('lodash/object/keys');
22+
23+
var _lodashObjectKeys2 = _interopRequireDefault(_lodashObjectKeys);
24+
2125
function initGoogleAnalytics(id) {
22-
if (window.ga) {
23-
return;
24-
}
26+
var set = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
2527

26-
if (!id) {
27-
throw new Error('Google analytics ID is undefined');
28+
if (window.ga || !id) {
29+
return;
2830
}
2931

3032
window.ga = window.ga || function () {
@@ -42,6 +44,12 @@ function initGoogleAnalytics(id) {
4244
})();
4345

4446
window.ga('create', id, 'auto');
47+
48+
(0, _lodashObjectKeys2['default'])(set).forEach(function (key) {
49+
var value = set[key];
50+
51+
window.ga('set', key, value);
52+
});
4553
}
4654

4755
var GoogleAnalytics = (function (_Component) {
@@ -58,7 +66,7 @@ var GoogleAnalytics = (function (_Component) {
5866
value: function componentDidMount() {
5967
var _this = this;
6068

61-
initGoogleAnalytics(this.props.id);
69+
initGoogleAnalytics(this.props.id, this.props.set);
6270

6371
this.historyListener = this.context.history.listen(function (err, renderProps) {
6472
if (err || !renderProps) {
@@ -96,7 +104,7 @@ var GoogleAnalytics = (function (_Component) {
96104
this.latestUrl = path;
97105

98106
// wait for correct title
99-
setTimeout(function wait() {
107+
setTimeout(function () {
100108
GoogleAnalytics.sendPageview(path, document.title);
101109
}, 0);
102110
}
@@ -134,13 +142,14 @@ var GoogleAnalytics = (function (_Component) {
134142
}, {
135143
key: 'propTypes',
136144
value: {
137-
id: _react2['default'].PropTypes.string.isRequired
145+
id: _react.PropTypes.string,
146+
set: _react.PropTypes.object
138147
},
139148
enumerable: true
140149
}, {
141150
key: 'contextTypes',
142151
value: {
143-
history: _react2['default'].PropTypes.object.isRequired
152+
history: _react.PropTypes.object.isRequired
144153
},
145154
enumerable: true
146155
}]);

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-g-analytics",
3-
"version": "0.1.16",
3+
"version": "0.1.17",
44
"description": "React google analytics with support for react-router",
55
"author": {
66
"name": "Zlatko Fedor",
@@ -31,12 +31,14 @@
3131
"build": "babel-node ./node_modules/gulp/bin/gulp.js build",
3232
"eslint": "node ./node_modules/eslint/bin/eslint.js --ext .js,.jsx ."
3333
},
34-
"dependencies": {},
34+
"dependencies": {
35+
"lodash": "^3.10.1"
36+
},
3537
"devDependencies": {
3638
"babel": "^5.8.34",
3739
"babel-eslint": "^4.1.5",
3840
"eslint": "^1.10.1",
39-
"eslint-config-airbnb": "^1.0.0",
41+
"eslint-config-airbnb": "^0.1.1",
4042
"eslint-loader": "^1.1.1",
4143
"eslint-plugin-react": "^3.10.0",
4244
"gulp": "^3.9.0",

src/index.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import React, { Component } from 'react';
1+
import React, { Component, PropTypes } from 'react';
2+
import keys from 'lodash/object/keys';
23

3-
function initGoogleAnalytics(id) {
4-
if (window.ga) {
4+
function initGoogleAnalytics(id, set = {}) {
5+
if (window.ga || !id) {
56
return;
67
}
78

8-
if (!id) {
9-
throw new Error('Google analytics ID is undefined');
10-
}
11-
129
window.ga = window.ga || function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; // eslint-disable-line
1310

1411
(function loadScript() {
@@ -22,19 +19,26 @@ function initGoogleAnalytics(id) {
2219
})();
2320

2421
window.ga('create', id, 'auto');
22+
23+
keys(set).forEach((key) => {
24+
const value = set[key];
25+
26+
window.ga('set', key, value);
27+
});
2528
}
2629

2730
export default class GoogleAnalytics extends Component {
2831
static propTypes = {
29-
id: React.PropTypes.string.isRequired,
32+
id: PropTypes.string,
33+
set: PropTypes.object,
3034
};
3135

3236
static contextTypes = {
33-
history: React.PropTypes.object.isRequired,
37+
history: PropTypes.object.isRequired,
3438
};
3539

3640
componentDidMount() {
37-
initGoogleAnalytics(this.props.id);
41+
initGoogleAnalytics(this.props.id, this.props.set);
3842

3943
this.historyListener = this.context.history.listen((err, renderProps) => {
4044
if (err || !renderProps) {
@@ -67,7 +71,7 @@ export default class GoogleAnalytics extends Component {
6771
this.latestUrl = path;
6872

6973
// wait for correct title
70-
setTimeout(function wait() {
74+
setTimeout(() => {
7175
GoogleAnalytics.sendPageview(path, document.title);
7276
}, 0);
7377
}

0 commit comments

Comments
 (0)