Skip to content

Commit

Permalink
Code shuffle, eslint errors and re-factor
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Sep 1, 2021
1 parent bf0f3fc commit 33076a9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 55 deletions.
32 changes: 15 additions & 17 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ class Analytics {
!this.autoTrackHandlersRegistered
) {
this.autoTrackFeatureEnabled = true;
addDomEventHandlers(this);
this.autoTrackHandlersRegistered = true;
this.registerAutoTrackHandlers();
}
response.source.destinations.forEach(function (destination, index) {
// logger.debug(
Expand Down Expand Up @@ -266,10 +265,18 @@ class Analytics {
// "autoTrackHandlersRegistered",
// this.autoTrackHandlersRegistered
// );
if (this.autoTrackFeatureEnabled && !this.autoTrackHandlersRegistered) {
addDomEventHandlers(this);
this.autoTrackHandlersRegistered = true;
}
this.registerAutoTrackHandlers();
}
}

registerAutoTrackHandlers() {
if (this.autoTrackFeatureEnabled && !this.autoTrackHandlersRegistered) {
addDomEventHandlers(this);
this.autoTrackHandlersRegistered = true;
// logger.debug(
// "autoTrackHandlersRegistered",
// this.autoTrackHandlersRegistered
// );
}
}

Expand Down Expand Up @@ -1022,21 +1029,12 @@ class Analytics {
}
if (options && options.useAutoTracking) {
this.autoTrackFeatureEnabled = true;
if (this.autoTrackFeatureEnabled && !this.autoTrackHandlersRegistered) {
addDomEventHandlers(this);
this.autoTrackHandlersRegistered = true;
// logger.debug(
// "autoTrackHandlersRegistered",
// this.autoTrackHandlersRegistered
// );
}
this.registerAutoTrackHandlers();
}

function errorHandler(error) {
handleError(error);
if (this.autoTrackFeatureEnabled && !this.autoTrackHandlersRegistered) {
addDomEventHandlers(this);
}
this.registerAutoTrackHandlers();
}

if (options && options.cdnBaseURL) {
Expand Down
76 changes: 38 additions & 38 deletions utils/autotrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ function registerEvent(element, type, handler, useCapture) {
element.addEventListener(type, handler, !!useCapture);
}

function isTag(el, tag) {
return el && el.tagName && el.tagName.toLowerCase() === tag.toLowerCase();
}

function isElementNode(el) {
return el && el.nodeType === 1; // Node.ELEMENT_NODE - use integer constant for browser portability
}

function isTextNode(el) {
return el && el.nodeType === 3; // Node.TEXT_NODE - use integer constant for browser portability
}

function shouldTrackDomEvent(el, event) {
if (!el || isTag(el, "html") || !isElementNode(el)) {
return false;
Expand All @@ -35,16 +47,16 @@ function shouldTrackDomEvent(el, event) {
}
}

function isTag(el, tag) {
return el && el.tagName && el.tagName.toLowerCase() === tag.toLowerCase();
}

function isElementNode(el) {
return el && el.nodeType === 1; // Node.ELEMENT_NODE - use integer constant for browser portability
}

function isTextNode(el) {
return el && el.nodeType === 3; // Node.TEXT_NODE - use integer constant for browser portability
function getClassName(el) {
switch (typeof el.className) {
case "string":
return el.className;
case "object": // handle cases where className might be SVGAnimatedString or some other type
return el.className.baseVal || el.getAttribute("class") || "";
default:
// future proof
return "";
}
}

// excerpt from https://github.com/mixpanel/mixpanel-js/blob/master/src/autotrack-utils.js
Expand Down Expand Up @@ -116,18 +128,6 @@ function shouldTrackElement(el) {
return true;
}

function getClassName(el) {
switch (typeof el.className) {
case "string":
return el.className;
case "object": // handle cases where className might be SVGAnimatedString or some other type
return el.className.baseVal || el.getAttribute("class") || "";
default:
// future proof
return "";
}
}

function isExplicitNoTrack(el) {
const classes = getClassName(el).split(" ");
if (classes.indexOf("rudder-no-track") >= 0) {
Expand Down Expand Up @@ -182,7 +182,7 @@ function isValueToBeTracked(value) {
*/
function isValueToBeTrackedFromTrackingList(el, includeList) {
const elAttributesLength = el.attributes.length;
for (let i = 0; i < elAttributesLength; i++) {
for (let i = 0; i < elAttributesLength; i += 1) {
const { value } = el.attributes[i];
if (includeList.indexOf(value) > -1) {
return true;
Expand Down Expand Up @@ -212,25 +212,35 @@ function getText(el) {
return text.trim();
}

function previousElementSibling(el) {
if (el.previousElementSibling) {
return el.previousElementSibling;
}
do {
el = el.previousSibling;
} while (el && !isElementNode(el));
return el;
}

function getPropertiesFromElement(elem, rudderanalytics) {
const props = {
classes: getClassName(elem).split(" "),
tag_name: elem.tagName.toLowerCase(),
};

const attrLength = elem.attributes.length;
for (let i = 0; i < attrLength; i++) {
for (let i = 0; i < attrLength; i += 1) {
const { name } = elem.attributes[i];
const { value } = elem.attributes[i];
if (value && isValueToBeTracked(value)) {
props[`attr__${name}`] = value;
}
if (
(name == "name" || name == "id") &&
(name === "name" || name === "id") &&
isValueToBeTrackedFromTrackingList(elem, rudderanalytics.trackValues)
) {
props.field_value =
name == "id"
name === "id"
? document.getElementById(value).value
: document.getElementsByName(value)[0].value;

Expand All @@ -244,9 +254,9 @@ function getPropertiesFromElement(elem, rudderanalytics) {
let nthOfType = 1;
let currentElem = elem;
while ((currentElem = previousElementSibling(currentElem))) {
nthChild++;
nthChild += 1;
if (currentElem.tagName === elem.tagName) {
nthOfType++;
nthOfType += 1;
}
}
props.nth_child = nthChild;
Expand All @@ -255,16 +265,6 @@ function getPropertiesFromElement(elem, rudderanalytics) {
return props;
}

function previousElementSibling(el) {
if (el.previousElementSibling) {
return el.previousElementSibling;
}
do {
el = el.previousSibling;
} while (el && !isElementNode(el));
return el;
}

function trackWindowEvent(e, rudderanalytics) {
let target = e.target || e.srcElement;
if (isTextNode(target)) {
Expand Down

0 comments on commit 33076a9

Please sign in to comment.