Skip to content
This repository was archived by the owner on Aug 8, 2020. It is now read-only.

Commit 582b148

Browse files
committed
Initial commit
0 parents  commit 582b148

7 files changed

+208
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- '0.12'

README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Jekll-Store/Google-Analytics
2+
3+
[![Build Status](https://travis-ci.org/jekyll-store/google-analytics.svg?branch=master)](https://travis-ci.org/jekyll-store/google-analytics)
4+
5+
Google analytics plugin for [Jekyll-Store Engine](https://github.com/jekyll-store/engine).
6+
7+
## Usage
8+
9+
Requires that the google analytics javascript file to be loaded:
10+
11+
```html
12+
<script async src="//www.google-analytics.com/analytics.js"></script>
13+
```
14+
15+
## Actions
16+
17+
### setTrackingId
18+
19+
Set's Google Analytics tracking ID.
20+
21+
Example:
22+
23+
```javascript
24+
JekyllStoreEngine.Actions.setTrackingId({ id: 'UA-62379004-1' });
25+
```
26+
27+
### pageLoaded
28+
29+
Declare the page loaded.
30+
31+
Example:
32+
33+
```javascript
34+
JekyllStoreEngine.Actions.pageLoaded();
35+
```
36+
37+
### checkoutStep
38+
39+
Declare the page a checkout step.
40+
41+
Example:
42+
43+
```javascript
44+
JekyllStoreEngine.Actions.checkoutStep({ step: 2 });
45+
```
46+
47+
## Contributing
48+
49+
1. [Fork it](https://github.com/jekyll-store/google-analytics/fork)
50+
2. Create your feature branch (`git checkout -b my-new-feature`)
51+
3. Commit your changes (`git commit -am 'Add some feature'`)
52+
4. Push to the branch (`git push origin my-new-feature`)
53+
5. Create a new Pull Request

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "jekyll-store-google-analytics",
3+
"main": "./src/JekyllStoreGoogleAnalytics.js",
4+
"version": "0.0.1",
5+
"description": "Google analytics plugin for Jekyll-Store Engine",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/jekyll-store/google-analytics.git"
9+
},
10+
"dependencies": {
11+
"big.js": "^3.0.1",
12+
"immutable": "^3.6.4",
13+
"reflux": "^0.2.7",
14+
"ga-browser": "^1.0.0"
15+
},
16+
"devDependencies": {
17+
"chai": "^2.1.2",
18+
"mocha": "^2.2.1",
19+
"sinon": "^1.14.1"
20+
},
21+
"peerDependencies": {
22+
"jekyll-store-engine": ">=0.1.3"
23+
},
24+
"scripts": {
25+
"test": "mocha"
26+
},
27+
"author": "Max White",
28+
"license": "ISC"
29+
}

src/GoogleAnalyticsStore.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Includes
2+
var Reflux = require('reflux');
3+
var JSE = require('jekyll-store-engine');
4+
var listenAndMix = JSE.Mixins.listenAndMix;
5+
6+
var GoogleAnalyticsStore = Reflux.createStore({
7+
// Public
8+
listenables: [JSE.Actions],
9+
mixins: [
10+
listenAndMix(JSE.Stores.Basket),
11+
listenAndMix(JSE.Stores.Delivery),
12+
listenAndMix(JSE.Stores.Order)
13+
],
14+
onSetTrackingId: function(args) {
15+
t.ga('create', args.id, 'auto');
16+
t.ga('require', 'ec');
17+
},
18+
onPageLoaded: function() {
19+
t.ga('send', 'pageview');
20+
},
21+
onVisit: function(args) {
22+
t.ga('ec:addProduct', { 'name': args.name });
23+
t.ga('ec:setAction', 'detail');
24+
},
25+
onSetItem: function(args) {
26+
t.ga('ec:addProduct', { 'name': args.name, 'quantity': args.quantity });
27+
t.ga('ec:setAction', 'add');
28+
t.ga('send', 'event', 'UX', 'click', 'add to basket');
29+
},
30+
onRemoveItem: function(args) {
31+
t.ga('ec:addProduct', { 'name': args.name });
32+
t.ga('ec:setAction', 'remove');
33+
t.ga('send', 'event', 'UX', 'click', 'remove from basket');
34+
},
35+
onCheckoutStep: function(args) {
36+
t.addBasket();
37+
t.ga('ec:setAction','checkout', { 'step': args.step });
38+
},
39+
onSetPaymentOptions: function(args) {
40+
t.ga('set', '&cu', args.currency);
41+
},
42+
onCompleted: function(args) {
43+
t.addBasket();
44+
t.ga('ec:setAction', 'purchase', {
45+
'id': args.number,
46+
'revenue': t.order.getIn(['totals', 'order']).toString(),
47+
'shipping': t.delivery.get('amount').toString()
48+
});
49+
t.ga('send', 'pageview');
50+
},
51+
52+
// Private
53+
ga: require('ga-browser')(),
54+
addBasket: function() {
55+
t.basket.forEach(function(item, name) {
56+
t.ga('ec:addProduct', {
57+
'name': name,
58+
'price': item.get('price').toString(),
59+
'quantity': item.get('quantity').toString()
60+
});
61+
});
62+
}
63+
});
64+
65+
var t = module.exports = GoogleAnalyticsStore;

src/JekyllStoreGoogleAnalytics.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var Reflux = require('reflux');
2+
var JSE = require('jekyll-store-engine');
3+
4+
JSE.Actions.setTrackingId = Reflux.createAction();
5+
JSE.Actions.pageLoaded = Reflux.createAction();
6+
JSE.Actions.visit = JSE.Actions.visit || Reflux.createAction();
7+
JSE.Actions.checkoutStep = Reflux.createAction();
8+
JSE.Stores.GoogleAnalytics = require('./GoogleAnalyticsStore');

test/GoogleAnalyticsStore.spec.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var assert = require('chai').assert;
2+
var sinon = require('sinon');
3+
var I = require('immutable');
4+
var B = require('big.js');
5+
var GoogleAnalyticsStore = require('../src/GoogleAnalyticsStore');
6+
7+
describe('GoogleAnalyticsStore', function() {
8+
GoogleAnalyticsStore.ga = sinon.spy();
9+
10+
GoogleAnalyticsStore.basket = I.fromJS({
11+
'Laser Sight': { name: 'Laser Sight', price: B(34.28), quantity: B(2) },
12+
'Bayonet': { name: 'Bayonet', price: B(8.75), quantity: B(1) }
13+
});
14+
15+
GoogleAnalyticsStore.delivery = I.fromJS({ name: 'UPS Extra', amount: B(4.50) });
16+
17+
GoogleAnalyticsStore.order = I.fromJS({
18+
totals: { price: B(77.31), order: B(81.81) },
19+
delivery: 'UPS Extra',
20+
errors: [],
21+
adjustments: [{ label: 'UPS Extra', amount: B(4.50) }]
22+
});
23+
24+
it('adds basket and purchase action on completion', function() {
25+
GoogleAnalyticsStore.onSetTrackingId({ id: 'UA-62379004-1' });
26+
GoogleAnalyticsStore.onCompleted({ number: 'FHRUDH458DU' });
27+
var expected = [
28+
['create', 'UA-62379004-1', 'auto'],
29+
['require', 'ec'],
30+
['ec:addProduct', {
31+
'name': 'Laser Sight',
32+
'price': '34.28',
33+
'quantity': '2'
34+
}],
35+
['ec:addProduct', {
36+
'name': 'Bayonet',
37+
'price': '8.75',
38+
'quantity': '1'
39+
}],
40+
['ec:setAction', 'purchase', {
41+
'id': 'FHRUDH458DU',
42+
'revenue': '81.81',
43+
'shipping': '4.5'
44+
}],
45+
['send', 'pageview' ]
46+
]
47+
assert.deepEqual(GoogleAnalyticsStore.ga.args, expected);
48+
});
49+
});

0 commit comments

Comments
 (0)