This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
307 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
# Tests cannot be run from metarepo - it's a public repository, which doesn't have access to private repositories on Travis (for security reasons). | ||
# The actual test setup is in Page Builder bundle. | ||
language: php | ||
php: | ||
- '5.6' | ||
language: node_js | ||
|
||
node_js: | ||
- '11' | ||
|
||
install: | ||
- npm install | ||
|
||
script: | ||
- echo 'Test suite moved to PageBuilder bundle' | ||
- npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "ezplatform.com", | ||
"version": "2.3.0", | ||
"author": "eZ Systems", | ||
"homepage": "ez.no", | ||
"scripts": { | ||
"test": "mocha ./test/*.spec.js" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"chai-dom": "^1.8.1", | ||
"chai-subset": "^1.6.0", | ||
"eslint": "^5.10.0", | ||
"eslint-config-prettier": "^3.3.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"eslint-plugin-react": "^7.11.1", | ||
"jquery": "^3.3.1", | ||
"jsdom": "^13.0.0", | ||
"mocha": "^5.2.0", | ||
"prettier": "1.15.3", | ||
"sinon": "^7.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const expect = require('chai').use(require('chai-dom')).use(require('chai-subset')).expect; | ||
const sinon = require('sinon'); | ||
const { JSDOM } = require('jsdom'); | ||
const GoogleAnalyticsService = require('../web/assets/js/GoogleAnalyticsService'); | ||
|
||
describe('GoogleAnalyticsService', () => { | ||
let gaService; | ||
let document; | ||
let handleClick; | ||
|
||
const template = '/html/gaTemplate.html'; | ||
const gaDataAttr = 'data-ga-action'; | ||
const gaCategoryAttr = 'data-ga-category'; | ||
const gaLabelAttr = 'data-ga-label'; | ||
|
||
beforeEach(() => { | ||
gaService = new GoogleAnalyticsService(); | ||
handleClick = sinon.spy(); | ||
|
||
return JSDOM.fromFile(__dirname + template) | ||
.then(dom => { | ||
document = dom.window.document; | ||
document.addEventListener('click', handleClick, false); | ||
|
||
return document; | ||
}) | ||
.catch(error => { | ||
console.log(error); | ||
}); | ||
}); | ||
|
||
it('should return instance of GoogleAnalyticsService', () => { | ||
expect(gaService).to.be.instanceOf(GoogleAnalyticsService); | ||
}); | ||
|
||
it('should find selectors with data attribute data-ga-action', () => { | ||
expect(document.querySelectorAll(`[${gaDataAttr}]`)).not.to.have.lengthOf(0); | ||
}); | ||
|
||
it('should return value for each data-ga-action attribute', () => { | ||
const actions = document.querySelectorAll(`[${gaDataAttr}]`); | ||
|
||
actions.forEach(action => { | ||
expect(gaService.getGAAction(action), action.outerHTML).not.to.have.lengthOf(0); | ||
}); | ||
}); | ||
|
||
it('should return true if body has data-ga-category attribute', () => { | ||
expect(document.body).to.have.attribute(gaCategoryAttr); | ||
}); | ||
|
||
it('should return value for data-ga-category attribute', () => { | ||
expect(document.querySelector(`[${gaCategoryAttr}]`).getAttribute(gaCategoryAttr)).not.to.have.lengthOf(0); | ||
}); | ||
|
||
it('should return Google Analytics Label if data-ga-label attribute exists', () => { | ||
const labels = document.querySelectorAll(`[${gaLabelAttr}]`); | ||
|
||
labels.forEach(label => { | ||
expect(gaService.getGALabel(label), label.outerHTML).not.to.have.lengthOf(0); | ||
}); | ||
}); | ||
|
||
it('should rewrite data attributes to child element when ga-link-listener class exists', () => { | ||
const element = document.querySelector('.ga-link-listener'); | ||
const childElement = gaService.rewriteByChildElement(element); | ||
|
||
expect(childElement.dataset).containSubset(element.dataset); | ||
}); | ||
|
||
it('should create object after click on data-ga-action event', () => { | ||
const actions = document.querySelectorAll(`[${gaDataAttr}]`); | ||
let gaItem; | ||
|
||
actions.forEach(action => { | ||
action.addEventListener('click', () => { | ||
gaItem = new Object({ | ||
hitType: 'event', | ||
eventCategory: 'Downloads', | ||
eventAction: 'Download', | ||
eventLabel: 'Label', | ||
transport: 'Transport' | ||
}); | ||
}); | ||
action.click(); | ||
expect(gaItem).to.be.an('object').that.containSubset({ | ||
'hitType': 'event', | ||
'eventCategory': 'Downloads', | ||
'eventAction': 'Download', | ||
'eventLabel': 'Label', | ||
'transport': 'Transport', | ||
}); | ||
}); | ||
|
||
expect(handleClick.callCount).to.equal(actions.length); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head></head> | ||
<body data-ga-category="category-name"> | ||
<p data-ga-action="action1">Action name</p> | ||
<p data-ga-action="action2">Action name</p> | ||
<p data-ga-label="label">Label</p> | ||
<p data-ga-label="label2">Label</p> | ||
<div class="ga-link-listener" data-ga-action="Action rewrite" data-ga-label="label"> | ||
<a href="#">Test</a> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
class GoogleAnalyticsService { | ||
|
||
constructor () { | ||
this.gaDataAttr = 'data-ga-action'; | ||
this.gaCategoryAttr = 'data-ga-category'; | ||
this.gaLabelAttr = 'data-ga-label'; | ||
} | ||
|
||
rewriteByChildElement (element) { | ||
if (element.classList.contains('ga-link-listener')) { | ||
const childElement = element.querySelector('a'); | ||
|
||
return this.rewriteDataAttributes(element, childElement); | ||
} | ||
|
||
return element; | ||
} | ||
|
||
getGAItemObject (element) { | ||
return { | ||
hitType: 'event', | ||
eventCategory: this.getGACategory(), | ||
eventAction: this.getGAAction(element), | ||
eventLabel: this.getGALabel(element) | ||
}; | ||
} | ||
|
||
getGAAction (element) { | ||
return element.getAttribute(this.gaDataAttr); | ||
} | ||
|
||
getGACategory () { | ||
return window.eZ.helpers.stringUtils.capitalize(document.body.getAttribute(this.gaCategoryAttr)); | ||
} | ||
|
||
getGALabel (element) { | ||
return element.getAttribute(this.gaLabelAttr); | ||
} | ||
|
||
sendGARequest (gaItem) { | ||
ga( | ||
'send', | ||
gaItem.hitType, | ||
gaItem.eventCategory, | ||
gaItem.eventAction, | ||
gaItem.eventLabel, | ||
gaItem.transport | ||
); | ||
} | ||
|
||
rewriteDataAttributes (parentElement, childElement) { | ||
childElement.setAttribute(this.gaDataAttr, this.getGAAction(parentElement)); | ||
childElement.setAttribute(this.gaLabelAttr, this.getGALabel(parentElement)); | ||
|
||
return childElement; | ||
} | ||
|
||
init () { | ||
const actions = document.querySelectorAll(`[${this.gaDataAttr}]`); | ||
|
||
actions.forEach(action => { | ||
action = this.rewriteByChildElement(action); | ||
action.addEventListener('click', (event) => { | ||
this.sendGARequest(this.getGAItemObject(event.currentTarget)); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = GoogleAnalyticsService; |
Oops, something went wrong.