@availity/analytics-core
Advanced tools
Comparing version 2.5.1 to 2.6.0
@@ -6,2 +6,19 @@ # Change Log | ||
# [2.6.0](https://github.com/Availity/sdk-js/compare/@availity/analytics-core@2.5.1...@availity/analytics-core@2.6.0) (2019-03-18) | ||
### Bug Fixes | ||
* **analytics-core:** added dynamic length of attr key for slicing ([18df607](https://github.com/Availity/sdk-js/commit/18df607)) | ||
* **analytics-core:** moved extra assignments out ([23130f1](https://github.com/Availity/sdk-js/commit/23130f1)) | ||
### Features | ||
* **analytics-core:** added config object with attribute prefix and recursive ([4b5b9fa](https://github.com/Availity/sdk-js/commit/4b5b9fa)) | ||
## [2.5.1](https://github.com/Availity/sdk-js/compare/@availity/analytics-core@2.5.0...@availity/analytics-core@2.5.1) (2019-02-12) | ||
@@ -8,0 +25,0 @@ |
{ | ||
"name": "@availity/analytics-core", | ||
"version": "2.5.1", | ||
"version": "2.6.0", | ||
"description": "Analytics base configuration for sdk-js", | ||
@@ -11,3 +11,3 @@ "main": "src/index.js", | ||
}, | ||
"gitHead": "9741ebcc564548d3f0e6def050b68529483b9350" | ||
"gitHead": "21b480acb7fc70e240d88451cee8adecb5ddde56" | ||
} |
@@ -22,5 +22,16 @@ # Analytics Core | ||
```javascript | ||
new AvAnalytics(plugins, promise, pageTracking, autoTrack); | ||
new AvAnalytics(plugins, promise, pageTracking, autoTrack, options); | ||
``` | ||
### Options | ||
- **attributePrefix** string. Overrides the default prefix for getting attributes. | ||
- **recursive** boolean. If `true`, will add on all attributes from the clicke/focused node up to the root element. It requires one attribute to have contain `action`. | ||
Example using the `recursive` option ( Will add all 3 attributes when the `anchor` tag is clicked. If the container is clicked nothing will happen ): | ||
```html | ||
<div class="container" data-analytics-app-name="app"> | ||
<a href="#" data-analytics-action="click" data-analytics-event-name="linking">Click me!</a> | ||
</div> | ||
``` | ||
## Plugins | ||
@@ -27,0 +38,0 @@ |
@@ -26,20 +26,31 @@ const isLeftClickEvent = event => event.button === 0; | ||
const getAnalyticAttrs = elem => { | ||
const attrs = [...elem.attributes]; | ||
const analyticAttrs = {}; | ||
/** | ||
* Polyfill for [`Event.composedPath()`][1]. | ||
* https://gist.github.com/kleinfreund/e9787d73776c0e3750dcfcdc89f100ec | ||
*/ | ||
const getComposedPath = node => { | ||
let parent; | ||
if (node.parentNode) { | ||
parent = node.parentNode; | ||
} else if (node.host) { | ||
parent = node.host; | ||
} else if (node.defaultView) { | ||
parent = node.defaultView; | ||
} | ||
if (elem.nodeType === 1) { | ||
for (let i = attrs.length - 1; i >= 0; i--) { | ||
const { name } = attrs[i]; | ||
if (name.indexOf('data-analytics-') === 0) { | ||
const camelName = camelCase(name.slice(15)); | ||
analyticAttrs[camelName] = elem.getAttribute(name); | ||
} | ||
} | ||
if (parent !== undefined) { | ||
return [node].concat(getComposedPath(parent)); | ||
} | ||
return analyticAttrs; | ||
return [node]; | ||
}; | ||
export default class AvAnalytics { | ||
constructor(plugins, promise = Promise, pageTracking, autoTrack = true) { | ||
constructor( | ||
plugins, | ||
promise = Promise, | ||
pageTracking, | ||
autoTrack = true, | ||
options = {} | ||
) { | ||
// if plugins or promise are undefined, | ||
@@ -54,2 +65,4 @@ // or if either is skipped and pageTracking boolean is used in their place | ||
this.Promise = promise; | ||
this.recursive = !!options.recursive; | ||
this.attributePrefix = options.attributePrefix || 'data-analytics'; | ||
@@ -67,15 +80,36 @@ this.isPageTracking = false; | ||
handleEvent = event => { | ||
if ( | ||
isModifiedEvent(event) || | ||
(event.type === 'click' && !isLeftClickEvent(event)) || | ||
!isValidEventTypeOnTarget(event) | ||
) { | ||
if (this.invalidEvent(event)) { | ||
return; | ||
} | ||
const target = event.target || event.srcElement; | ||
const analyticAttrs = getAnalyticAttrs(target); | ||
const path = getComposedPath(event.target); | ||
if (!Object.keys(analyticAttrs).length > 0) { | ||
let analyticAttrs = {}; | ||
if (this.recursive) { | ||
// Reverse the array so we pull attributes from top down | ||
path.reverse().forEach(pth => { | ||
const attrs = this.getAnalyticAttrs(pth); | ||
analyticAttrs = { ...analyticAttrs, ...attrs }; | ||
// To consider using the element it has to have analytics attrs | ||
if (Object.keys(attrs).length > 0) { | ||
analyticAttrs.elemId = | ||
pth.getAttribute('id') || pth.getAttribute('name'); | ||
} | ||
}); | ||
} else { | ||
analyticAttrs = this.getAnalyticAttrs(target); | ||
} | ||
if ( | ||
!Object.keys(analyticAttrs).length > 0 || | ||
(this.recursive && !analyticAttrs.action) | ||
) { | ||
return; | ||
} | ||
analyticAttrs.action = analyticAttrs.action || event.type; | ||
analyticAttrs.elemId = | ||
@@ -85,6 +119,33 @@ analyticAttrs.elemId || | ||
target.getAttribute('name'); | ||
analyticAttrs.action = analyticAttrs.action || event.type; | ||
this.trackEvent(analyticAttrs); | ||
}; | ||
validEvent = event => | ||
isModifiedEvent(event) || | ||
(event.type === 'click' && !isLeftClickEvent(event)) || | ||
!isValidEventTypeOnTarget(event); | ||
getAnalyticAttrs = elem => { | ||
if (!elem.attributes) { | ||
return {}; | ||
} | ||
const attrs = [...elem.attributes]; | ||
const analyticAttrs = {}; | ||
if (elem.nodeType === 1) { | ||
for (let i = attrs.length - 1; i >= 0; i--) { | ||
const { name } = attrs[i]; | ||
if (name.indexOf(`${this.attributePrefix}-`) === 0) { | ||
const camelName = camelCase( | ||
name.slice(this.attributePrefix.length + 1) | ||
); | ||
analyticAttrs[camelName] = elem.getAttribute(name); | ||
} | ||
} | ||
} | ||
return analyticAttrs; | ||
}; | ||
startPageTracking() { | ||
@@ -91,0 +152,0 @@ if (!this.pageListener) { |
@@ -35,2 +35,11 @@ import { AvAnalytics } from '..'; | ||
test('AvAnalytics should use custom configs', () => { | ||
mockAvAnalytics = new AvAnalytics([], Promise, true, true, { | ||
attributePrefix: 'some-attr', | ||
recursive: true, | ||
}); | ||
expect(mockAvAnalytics.attributePrefix).toBe('some-attr'); | ||
expect(mockAvAnalytics.recursive).toBe(true); | ||
}); | ||
describe('setPageTracking', () => { | ||
@@ -37,0 +46,0 @@ beforeEach(() => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23049
440
128