Skip to content

Commit

Permalink
Avoid mutating parameterArray
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Jan 23, 2025
1 parent c47703c commit 598b2fa
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions trackers/javascript-tracker/src/in_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,14 @@ export function InQueueManager(functionName: string, asyncQueue: Array<unknown>)
fnTrackers[tracker.id.replace(`${functionName}_`, '')] = tracker;
}

// Append the `fnTrackers` object to the parameterArray, to allow it to be
// accessed as the final argument in the callback, useful in environments that don't support `this` (GTM)
Array.prototype.push.call(parameterArray, fnTrackers);
// Create a new array from `parameterArray` to avoid mutating the original
const parameterArrayCopy = Array.prototype.slice.call(parameterArray);

input.apply(fnTrackers, parameterArray);
// Add the `fnTrackers` object as the last element of the new array to allow it to be accessed in the callback
// as the final argument, useful in environments that don't support `this` (GTM)
const args = Array.prototype.concat.apply(parameterArrayCopy, [fnTrackers]);

input.apply(fnTrackers, args);
} catch (ex) {
LOG.error('Tracker callback failed', ex);
} finally {
Expand Down

0 comments on commit 598b2fa

Please sign in to comment.