From 10429d92ff884a24ee63ed783ee23fb1328c8d05 Mon Sep 17 00:00:00 2001 From: Vinnie Freitas Date: Mon, 19 Mar 2018 18:04:46 -0700 Subject: [PATCH] WEB-1471:CHANGE: Internal registry to use objects instead of arrays. --- ga-task-manager.js | 8 ++++---- ga-task-manager.js.map | 2 +- lib/index.js | 23 ++++++++++------------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/ga-task-manager.js b/ga-task-manager.js index fd57906..ca925a0 100644 --- a/ga-task-manager.js +++ b/ga-task-manager.js @@ -1,5 +1,5 @@ -(function(){function e(a){var b=this;this.c=a;this.a={};this.b="customTask previewTask checkProtocolTask validationTask checkStorageTask historyImportTask samplerTask buildHitTask sendHitTask timingTask displayFeaturesTask".split(" ");this.addFunctionToTask=this.addFunctionToTask.bind(this);this.removeFunctionFromTask=this.removeFunctionFromTask.bind(this);this.setCustomDimension=this.setCustomDimension.bind(this);this.unsetCustomDimension=this.unsetCustomDimension.bind(this);this.b.forEach(function(c){b.a[c]= -[];b.addFunctionToTask(c,"original",a.get(c));b.c.set(c,function(a){b.a[c].forEach(function(b){b.f(a)})})})}e.prototype.addFunctionToTask=function(a,b,c){this.a[a]&&this.a[a].push({name:b,f:c})};e.prototype.removeFunctionFromTask=function(a,b){if(this.a[a]){var c=this.a[a].findIndex(function(a){return a.name===b});-1 {\n this.registry[gaTaskName] = [];\n // Grab a reference to the default function for this task and register it as the first task.\n this.addFunctionToTask(gaTaskName, 'original', tracker.get(gaTaskName));\n\n // Override the GA Tracker's task with our task manager executor.\n this.tracker.set(gaTaskName, (model) => {\n this.registry[gaTaskName].forEach((userTask) => {\n userTask.func(model);\n });\n });\n });\n }\n\n /**\n * Adds a function to be executed at the specified GA Task.\n * @param {string} gaTaskName The name of the GA Task to add the taskFunction to.\n * @param {string} userTaskName An arbitrary name for the task performed by the taskFunction.\n * @param {function(!Model)} taskFunction The function to be added to the execution stack of the GA Task specified in gaTaskName.\n * The GA Model Object (https://developers.google.com/analytics/devguides/collection/analyticsjs/model-object-reference) will be passed to it at time of execution.\n */\n addFunctionToTask(gaTaskName, userTaskName, taskFunction) {\n if (this.registry[gaTaskName]) {\n this.registry[gaTaskName].push({name: userTaskName, func: taskFunction});\n }\n }\n\n /**\n * Removes a function from the specified GA Task by the name given by the user.\n * @param {string} gaTaskName The name of the GA Task to remove the task from.\n * @param {string} userTaskName The arbitrary name for the task given to addFunctionToTask.\n */\n removeFunctionFromTask(gaTaskName, userTaskName) {\n if (this.registry[gaTaskName]) {\n var userTaskIndex = this.registry[gaTaskName].findIndex(function(userTask){\n return userTask.name === userTaskName;\n });\n if (userTaskIndex > -1) {\n this.registry[gaTaskName].splice(userTaskIndex, 1);\n }\n }\n }\n\n /**\n * Adds a function which sets a GA Custom Dimension at the specified GA Task execution time. Defaults to execution on the customTask Task.\n * @param {string} index The index of your dimension as defined in your Google Analytics property settings.\n * @param {string|number|function(): string|number} value Arbitrary string, number or function to set value for the dimension.\n * If a function is given, it will be executed every time at the moment of task execution.\n * If a number is given, it will be cast automatically to a string by calling Number.toString() method.\n * Observe size limit for the string value https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#customs\n * @param {string} gaTaskName The name of the GA Task on which to set the Custom Dimension. Defaults to 'customTask'.\n */\n setCustomDimension(index, value, gaTaskName = 'customTask') {\n const userTaskName = 'customDimension' + index;\n this.addFunctionToTask(gaTaskName, userTaskName, function(model){\n let auxValue = value;\n // If user provided a function to set value at execution time, then type check itsreturn value.\n if (typeof value == 'function') {\n auxValue = value();\n const auxType = typeof auxValue;\n if (auxType != 'string' && auxType != 'number') {\n throw new Error('Function ' + value.name + ' must return a string or number. Got ' + auxType + 'instead.');\n }\n }\n\n if (typeof auxValue === 'number') auxValue = auxValue.toString();\n\n // Set the dimension value to be sent to GA.\n model.set('dimension' + index, auxValue);\n });\n }\n\n /**\n * Removes a function added with setCustomDimension.\n * @param {string} index The index of your dimension as defined in your Google Analytics property settings.\n * @param {string} gaTaskName The name of the GA Task to add the taskFunction to.\n */\n unsetCustomDimension(index, gaTaskName = 'customTask') {\n const userTaskName = 'customDimension' + index;\n this.removeFunctionFromTask(gaTaskName, userTaskName);\n }\n\n /**\n * Resets all Tasks of the Tracker to the original function registered at the moment the plugin was required.\n */\n remove() {\n this.gaTaskNames.forEach((gaTaskName) => {\n const originalTaskFunction = this.registry[gaTaskName].find((element) => {\n return element.name == 'original';\n });\n this.tracker.set(gaTaskName, originalTaskFunction);\n });\n }\n}\n\nprovide('GaTaskManager', GaTaskManager);\n","/**\n * Copyright 2016 Google Inc. All Rights Reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * Modifications Copyright (C) 2017 Anki, Inc\n */\n\n\nimport {capitalize} from './utilities';\n\n\n/**\n * Provides a plugin for use with analytics.js, accounting for the possibility\n * that the global command queue has been renamed or not yet defined.\n * @param {string} pluginName The plugin name identifier.\n * @param {Function} pluginConstructor The plugin constructor function.\n */\nexport default function provide(pluginName, pluginConstructor) {\n const gaAlias = window.GoogleAnalyticsObject || 'ga';\n window[gaAlias] = window[gaAlias] || function(...args) {\n (window[gaAlias].q = window[gaAlias].q || []).push(args);\n };\n\n // Formally provides the plugin for use with analytics.js.\n window[gaAlias]('provide', pluginName, pluginConstructor);\n\n // Registers the plugin on the global gaplugins object.\n window.gaplugins = window.gaplugins || {};\n window.gaplugins[capitalize(pluginName)] = pluginConstructor;\n}\n","/**\n * Copyright 2016 Google Inc. All Rights Reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * Modifications Copyright (C) 2018 Anki, Inc.\n */\n\n/**\n * A small shim of Object.assign that aims for brevity over spec-compliant\n * handling all the edge cases.\n * @param {!Object} target The target object to assign to.\n * @param {...?Object} sources Additional objects who properties should be\n * assigned to target. Non-objects are converted to objects.\n * @return {!Object} The modified target object.\n */\nexport const assign = Object.assign || function(target, ...sources) {\n for (let i = 0, len = sources.length; i < len; i++) {\n const source = Object(sources[i]);\n for (let key in source) {\n if (Object.prototype.hasOwnProperty.call(source, key)) {\n target[key] = source[key];\n }\n }\n }\n return target;\n};\n\n\n/**\n * Capitalizes the first letter of a string.\n * @param {string} str The input string.\n * @return {string} The capitalized string\n */\nexport function capitalize(str) {\n return str.charAt(0).toUpperCase() + str.slice(1);\n}\n"]} \ No newline at end of file +{"version":3,"sources":["lib/index.js","lib/provide.js","lib/utilities.js"],"names":["constructor","GaTaskManager","tracker","registry","gaTaskNames","addFunctionToTask","bind","removeFunctionFromTask","setCustomDimension","unsetCustomDimension","forEach","gaTaskName","get","set","model","userTask","hasOwnProperty","userTaskName","taskFunction","index","value","auxValue","auxType","Error","name","toString","remove","originalTaskFunction","find","element","provide","pluginName","pluginConstructor","gaAlias","window","GoogleAnalyticsObject","push","q","args","gaplugins","charAt","toUpperCase","slice"],"mappings":"A,YAWEA,QALIC,EAKO,CAACC,CAAD,CAAU,CAAA,IAAA,EAAA,IACnB,KAAAA,EAAA,CAAeA,CACf,KAAAC,EAAA,CAAgB,EAEhB,KAAAC,EAAA,CAAmB,gKAAA,MAAA,CAAA,GAAA,CAenB,KAAAC,kBAAA,CAAyB,IAAAA,kBAAAC,KAAA,CAA4B,IAA5B,CACzB,KAAAC,uBAAA,CAA8B,IAAAA,uBAAAD,KAAA,CAAiC,IAAjC,CAC9B,KAAAE,mBAA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A,CAAOD,CAAP,CAAlB,EAAqC,QAAA,CAAS,CAAT,CAAkB,CAAT,IAAA,IAAA,EAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,SAAA,OAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,SAAA,CAAA,CAAA,CAC5CG,EAACF,MAAA,CAAOD,CAAP,CAAAI,EAADD,CAAqBF,MAAA,CAAOD,CAAP,CAAAI,EAArBD,EAA0C,EAA1CA,MAAA,CADqDE,CACrD,CADqD,CAKvDJ,OAAA,CAAOD,CAAP,CAAA,CAAgB,SAAhB,CAA2BF,CAA3B,CAAuCC,CAAvC,CAGAE,OAAAK,UAAA,CAAmBL,MAAAK,UAAnB,EAAuC,EACvCL,OAAAK,UAAA,CAA4BR,CCMrBS,OAAA,CAAW,CAAX,CAAAC,YAAA,EDNP,CAA4BV,CCMSW,MAAA,CAAU,CAAV,CDNrC,CAAA,CAA2CV,CAXkB,CAA/DF,CDmGA,CAAQ,eAAR,CAAyB7B,CAAzB","file":"","sourcesContent":["import provide from './provide';\n\n/**\n * Class for the `ga-task-manager` analytics.js plugin.\n * @implements {GaTaskManagerPublicInterface}\n */\nclass GaTaskManager {\n /**\n * Registers declarative event tracking.\n * @param {!Tracker} tracker Passed internally by analytics.js\n */\n constructor(tracker) {\n this.tracker = tracker;\n this.registry = {};\n // @see https://developers.google.com/analytics/devguides/collection/analyticsjs/tasks\n this.gaTaskNames = [\n 'customTask',\n 'previewTask',\n 'checkProtocolTask',\n 'validationTask',\n 'checkStorageTask',\n 'historyImportTask',\n 'samplerTask',\n 'buildHitTask',\n 'sendHitTask',\n 'timingTask',\n 'displayFeaturesTask'\n ];\n\n // Bind methods.\n this.addFunctionToTask = this.addFunctionToTask.bind(this);\n this.removeFunctionFromTask = this.removeFunctionFromTask.bind(this);\n this.setCustomDimension = this.setCustomDimension.bind(this);\n this.unsetCustomDimension = this.unsetCustomDimension.bind(this);\n\n this.gaTaskNames.forEach((gaTaskName) => {\n this.registry[gaTaskName] = {};\n // Grab a reference to the default function for this task and register it as the first task.\n this.addFunctionToTask(gaTaskName, 'original', tracker.get(gaTaskName));\n\n // Override the GA Tracker's task with our task manager executor which calls all user tasks added to the registry.\n this.tracker.set(gaTaskName, (model) => {\n for (var userTask in this.registry[gaTaskName]) {\n if (this.registry[gaTaskName].hasOwnProperty(userTask)) {\n this.registry[gaTaskName][userTask](model);\n }\n }\n });\n });\n }\n\n /**\n * Adds a function to be executed at the specified GA Task.\n * @param {string} gaTaskName The name of the GA Task to add the taskFunction to.\n * @param {string} userTaskName An arbitrary name for the task performed by the taskFunction.\n * @param {function(!Model)} taskFunction The function to be added to the execution stack of the GA Task specified in gaTaskName.\n * The GA Model Object (https://developers.google.com/analytics/devguides/collection/analyticsjs/model-object-reference) will be passed to it at time of execution.\n */\n addFunctionToTask(gaTaskName, userTaskName, taskFunction) {\n if (this.registry[gaTaskName]) {\n this.registry[gaTaskName][userTaskName] = taskFunction;\n }\n }\n\n /**\n * Removes a function from the specified GA Task by the name given by the user.\n * @param {string} gaTaskName The name of the GA Task to remove the task from.\n * @param {string} userTaskName The arbitrary name for the task given to addFunctionToTask.\n */\n removeFunctionFromTask(gaTaskName, userTaskName) {\n if (this.registry[gaTaskName] && this.registry[gaTaskName][userTaskName]) {\n delete this.registry[gaTaskName][userTaskName];\n }\n }\n\n /**\n * Adds a function which sets a GA Custom Dimension at the specified GA Task execution time. Defaults to execution on the customTask Task.\n * @param {string} index The index of your dimension as defined in your Google Analytics property settings.\n * @param {string|number|function(): string|number} value Arbitrary string, number or function to set value for the dimension.\n * If a function is given, it will be executed every time at the moment of task execution.\n * If a number is given, it will be cast automatically to a string by calling Number.toString() method.\n * Observe size limit for the string value https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#customs\n * @param {string} gaTaskName The name of the GA Task on which to set the Custom Dimension. Defaults to 'customTask'.\n */\n setCustomDimension(index, value, gaTaskName = 'customTask') {\n const userTaskName = 'customDimension' + index;\n this.addFunctionToTask(gaTaskName, userTaskName, function(model){\n let auxValue = value;\n // If user provided a function to set value at execution time, then type check itsreturn value.\n if (typeof value == 'function') {\n auxValue = value();\n const auxType = typeof auxValue;\n if (auxType != 'string' && auxType != 'number') {\n throw new Error('Function ' + value.name + ' must return a string or number. Got ' + auxType + 'instead.');\n }\n }\n\n if (typeof auxValue === 'number') auxValue = auxValue.toString();\n\n // Set the dimension value to be sent to GA.\n model.set('dimension' + index, auxValue);\n });\n }\n\n /**\n * Removes a function added with setCustomDimension.\n * @param {string} index The index of your dimension as defined in your Google Analytics property settings.\n * @param {string} gaTaskName The name of the GA Task to add the taskFunction to.\n */\n unsetCustomDimension(index, gaTaskName = 'customTask') {\n const userTaskName = 'customDimension' + index;\n this.removeFunctionFromTask(gaTaskName, userTaskName);\n }\n\n /**\n * Resets all Tasks of the Tracker to the original function registered at the moment the plugin was required.\n */\n remove() {\n this.gaTaskNames.forEach((gaTaskName) => {\n const originalTaskFunction = this.registry[gaTaskName].find((element) => {\n return element.name == 'original';\n });\n this.tracker.set(gaTaskName, originalTaskFunction);\n });\n }\n}\n\nprovide('GaTaskManager', GaTaskManager);\n","/**\n * Copyright 2016 Google Inc. All Rights Reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * Modifications Copyright (C) 2017 Anki, Inc\n */\n\n\nimport {capitalize} from './utilities';\n\n\n/**\n * Provides a plugin for use with analytics.js, accounting for the possibility\n * that the global command queue has been renamed or not yet defined.\n * @param {string} pluginName The plugin name identifier.\n * @param {Function} pluginConstructor The plugin constructor function.\n */\nexport default function provide(pluginName, pluginConstructor) {\n const gaAlias = window.GoogleAnalyticsObject || 'ga';\n window[gaAlias] = window[gaAlias] || function(...args) {\n (window[gaAlias].q = window[gaAlias].q || []).push(args);\n };\n\n // Formally provides the plugin for use with analytics.js.\n window[gaAlias]('provide', pluginName, pluginConstructor);\n\n // Registers the plugin on the global gaplugins object.\n window.gaplugins = window.gaplugins || {};\n window.gaplugins[capitalize(pluginName)] = pluginConstructor;\n}\n","/**\n * Copyright 2016 Google Inc. All Rights Reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * Modifications Copyright (C) 2018 Anki, Inc.\n */\n\n/**\n * A small shim of Object.assign that aims for brevity over spec-compliant\n * handling all the edge cases.\n * @param {!Object} target The target object to assign to.\n * @param {...?Object} sources Additional objects who properties should be\n * assigned to target. Non-objects are converted to objects.\n * @return {!Object} The modified target object.\n */\nexport const assign = Object.assign || function(target, ...sources) {\n for (let i = 0, len = sources.length; i < len; i++) {\n const source = Object(sources[i]);\n for (let key in source) {\n if (Object.prototype.hasOwnProperty.call(source, key)) {\n target[key] = source[key];\n }\n }\n }\n return target;\n};\n\n\n/**\n * Capitalizes the first letter of a string.\n * @param {string} str The input string.\n * @return {string} The capitalized string\n */\nexport function capitalize(str) {\n return str.charAt(0).toUpperCase() + str.slice(1);\n}\n"]} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 5ce830a..9210b26 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,15 +34,17 @@ class GaTaskManager { this.unsetCustomDimension = this.unsetCustomDimension.bind(this); this.gaTaskNames.forEach((gaTaskName) => { - this.registry[gaTaskName] = []; + this.registry[gaTaskName] = {}; // Grab a reference to the default function for this task and register it as the first task. this.addFunctionToTask(gaTaskName, 'original', tracker.get(gaTaskName)); - // Override the GA Tracker's task with our task manager executor. + // Override the GA Tracker's task with our task manager executor which calls all user tasks added to the registry. this.tracker.set(gaTaskName, (model) => { - this.registry[gaTaskName].forEach((userTask) => { - userTask.func(model); - }); + for (var userTask in this.registry[gaTaskName]) { + if (this.registry[gaTaskName].hasOwnProperty(userTask)) { + this.registry[gaTaskName][userTask](model); + } + } }); }); } @@ -56,7 +58,7 @@ class GaTaskManager { */ addFunctionToTask(gaTaskName, userTaskName, taskFunction) { if (this.registry[gaTaskName]) { - this.registry[gaTaskName].push({name: userTaskName, func: taskFunction}); + this.registry[gaTaskName][userTaskName] = taskFunction; } } @@ -66,13 +68,8 @@ class GaTaskManager { * @param {string} userTaskName The arbitrary name for the task given to addFunctionToTask. */ removeFunctionFromTask(gaTaskName, userTaskName) { - if (this.registry[gaTaskName]) { - var userTaskIndex = this.registry[gaTaskName].findIndex(function(userTask){ - return userTask.name === userTaskName; - }); - if (userTaskIndex > -1) { - this.registry[gaTaskName].splice(userTaskIndex, 1); - } + if (this.registry[gaTaskName] && this.registry[gaTaskName][userTaskName]) { + delete this.registry[gaTaskName][userTaskName]; } }