Skip to content

Commit 193844c

Browse files
committed
Create analytics service
1 parent edb13d4 commit 193844c

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
APP: {
3+
BASE_URL: 'http://localhost:7000'
4+
},
5+
ANALYTICS: {
6+
GOOGLE: {
7+
ID: 'UA-135973369-2',
8+
BASE_URL: 'https://www.googletagmanager.com/gtag/js'
9+
}
10+
}
11+
};

website/environments/production.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
APP: {
3+
BASE_URL: ''
4+
},
5+
ANALYTICS: {
6+
GOOGLE: {
7+
ID: 'UA-135973369-1',
8+
BASE_URL: 'https://www.googletagmanager.com/gtag/js'
9+
}
10+
}
11+
};

website/src/scripts/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import '@styles/_native.styl';
22
import Vue from 'vue';
33
import VueRouter from 'vue-router';
4+
import ENV from '@environment';
45
import routes from './routes';
56
import routeService from '@scripts/services/route/route';
7+
import analyticsService from '@scripts/services/analytics/analytics';
68
import template from './app.html';
79

810
Vue.use(VueRouter);
@@ -13,6 +15,7 @@ const router = new VueRouter({
1315
});
1416

1517
routeService.setRouter(router);
18+
analyticsService.init(ENV.ANALYTICS);
1619

1720
const app = {
1821
name: 'app',
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import dateService from '@scripts/services/date/date';
2+
3+
let ANALYTICS_ENV;
4+
const _public = {};
5+
6+
_public.init = analyticsEnv => {
7+
ANALYTICS_ENV = analyticsEnv;
8+
buildGoogleAnalyticsScriptTag(ANALYTICS_ENV.GOOGLE);
9+
configAnalytics(ANALYTICS_ENV.GOOGLE.ID);
10+
};
11+
12+
_public.trackPageView = path => {
13+
configAnalytics(ANALYTICS_ENV.GOOGLE.ID, path);
14+
};
15+
16+
function buildGoogleAnalyticsScriptTag(GOOGLE_ANALYTICS_ENV){
17+
const tag = document.createElement('script');
18+
tag.setAttribute('async', 'true');
19+
tag.setAttribute('src', `${GOOGLE_ANALYTICS_ENV.BASE_URL}?id=${GOOGLE_ANALYTICS_ENV.ID}`);
20+
document.head.appendChild(tag);
21+
}
22+
23+
function configAnalytics(id, path){
24+
if(!path)
25+
gtag('js', dateService.getNow());
26+
gtag('config', id, {page_path: (path || getLocationPath())});
27+
}
28+
29+
function getLocationPath(){
30+
return window.location.hash.replace('#/','/');
31+
}
32+
33+
function gtag(){
34+
window.dataLayer = window.dataLayer || [];
35+
window.dataLayer.push(arguments);
36+
}
37+
38+
export default _public;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import analyticsService from './analytics';
2+
import dateService from '@scripts/services/date/date';
3+
4+
describe('Analytics Service', () => {
5+
const dateMock = new Date();
6+
const locationHashMock = '#/home';
7+
const createElementMock = {
8+
setAttribute: jest.fn()
9+
};
10+
11+
function mockAnalyticsEnvironment(INSPECTLET = {}){
12+
return {
13+
GOOGLE: { ID: '123', BASE_URL: 'https://some.url.com' },
14+
INSPECTLET
15+
};
16+
}
17+
18+
beforeEach(() => {
19+
document.createElement = jest.fn(() => {
20+
return { setAttribute: createElementMock.setAttribute };
21+
});
22+
document.head.appendChild = jest.fn();
23+
dateService.getNow = jest.fn(() => dateMock);
24+
});
25+
26+
afterEach(() => {
27+
delete window.dataLayer;
28+
window.location.hash = '';
29+
});
30+
31+
it('should get analytics thirdy party code asynchronously', () => {
32+
analyticsService.init(mockAnalyticsEnvironment());
33+
expect(createElementMock.setAttribute).toHaveBeenCalledWith('async', 'true');
34+
});
35+
36+
it('should get analytics thirdy party code passing analytics id', () => {
37+
analyticsService.init(mockAnalyticsEnvironment());
38+
expect(createElementMock.setAttribute).toHaveBeenCalledWith(
39+
'src',
40+
'https://some.url.com?id=123'
41+
);
42+
});
43+
44+
it('should append script tag to get analytics thirdy party code on head', () => {
45+
analyticsService.init(mockAnalyticsEnvironment());
46+
expect(typeof document.head.appendChild.mock.calls[0][0]).toEqual('object');
47+
});
48+
49+
it('should configure analytics settings after append script tag on head', () => {
50+
window.location.hash = locationHashMock;
51+
analyticsService.init(mockAnalyticsEnvironment());
52+
expect(window.dataLayer[0][0]).toEqual('js');
53+
expect(window.dataLayer[0][1]).toEqual(dateMock);
54+
expect(window.dataLayer[1][0]).toEqual('config');
55+
expect(window.dataLayer[1][1]).toEqual('123');
56+
expect(window.dataLayer[1][2]).toEqual({page_path: '/home'});
57+
});
58+
59+
it('should track page view', () => {
60+
const path = '/author';
61+
analyticsService.trackPageView(path);
62+
expect(window.dataLayer[0][0]).toEqual('config');
63+
expect(window.dataLayer[0][1]).toEqual('123');
64+
expect(window.dataLayer[0][2]).toEqual({page_path: path});
65+
});
66+
});

0 commit comments

Comments
 (0)