Skip to content

Commit

Permalink
Implement enabled option (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Osvaldas Valutis authored Jun 10, 2020
1 parent 5aafb86 commit e831b5e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
74 changes: 57 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TrackingUtil {

// Default options
this.options = {
enabled: true,
cookie: {
name: `tracking-util-reacted`,
options: {
Expand Down Expand Up @@ -132,6 +133,14 @@ class TrackingUtil {
this.initGA()
}

/*
*
*
* GTM
*
*
*/

/*
* Checks if enough options provided to perform GTM tracking
*/
Expand All @@ -144,7 +153,11 @@ class TrackingUtil {
* Initiates GTM tracking
*/
initGTM() {
if (!this.isGTMtrackable() || !this.trackingAccepted()) {
if (
!this.options.enabled ||
!this.trackingAccepted() ||
!this.isGTMtrackable()
) {
return false
}

Expand Down Expand Up @@ -178,17 +191,20 @@ class TrackingUtil {
* @returns {bool} `true` on success and `false` on failure
*/
registerGTMdata(data) {
const { dataLayerName } = this.options.services.gtm

if (
!this.isGTMtrackable() ||
!this.options.enabled ||
!this.trackingAccepted() ||
typeof data !== `object` ||
typeof window[dataLayerName] !== `object`
!this.isGTMtrackable() ||
typeof data !== `object`
) {
return false
}

const { dataLayerName } = this.options.services.gtm
if (!Array.isArray(window[dataLayerName])) {
return false
}

window[dataLayerName].push(data)

return true
Expand All @@ -199,14 +215,30 @@ class TrackingUtil {
* @returns {array}
*/
registeredGTMdata() {
if (!this.isGTMtrackable()) {
return false
if (
!this.options.enabled ||
!this.trackingAccepted() ||
!this.isGTMtrackable()
) {
return []
}

const { dataLayerName } = this.options.services.gtm
return window[dataLayerName] || []
if (!Array.isArray(window[dataLayerName])) {
return []
}

return window[dataLayerName]
}

/*
*
*
* GA
*
*
*/

/*
* Checks if enough options provided to perform GA tracking
*/
Expand All @@ -219,7 +251,11 @@ class TrackingUtil {
* Initiates GA tracking
*/
initGA() {
if (!this.isGAtrackable() || !this.trackingAccepted()) {
if (
!this.options.enabled ||
!this.trackingAccepted() ||
!this.isGAtrackable()
) {
return false
}

Expand Down Expand Up @@ -248,7 +284,7 @@ class TrackingUtil {
)
/* eslint-enable */

window[ga.commandQueue](`create`, ga.id, ga.createFields)
this.runGAcommand([`create`, ga.id, ga.createFields])

if (Array.isArray(defaultGAcommands)) {
defaultGAcommands.forEach((d) => this.runGAcommand(d))
Expand All @@ -260,20 +296,24 @@ class TrackingUtil {
/*
* Runs GA command
*
* @param {array} data
* @param {array} data Command params
* @returns {bool} `true` on success and `false` on failure
*/
runGAcommand(data) {
const { commandQueue } = this.options.services.ga

if (
!this.isGAtrackable() ||
!this.options.enabled ||
!this.trackingAccepted() ||
!Array.isArray(data) ||
typeof window[commandQueue] !== `function`
!this.isGAtrackable() ||
!Array.isArray(data)
) {
return false
}

const { commandQueue } = this.options.services.ga
if (typeof window[commandQueue] !== `function`) {
return false
}

window[commandQueue](...data)

return true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@kollegorna/tracking-util",
"description": "GDPR compliant tracking",
"version": "1.0.4",
"version": "1.0.5",
"author": "Kollegorna <[email protected]>",
"license": "MIT",
"private": false,
Expand Down

0 comments on commit e831b5e

Please sign in to comment.