Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to use custom dataLayer variable #401

Merged
merged 8 commits into from
Feb 27, 2024

Conversation

eljass
Copy link
Contributor

@eljass eljass commented Feb 2, 2024

This pull request introduces the ability to change the name of the dataLayer used by GTM. The new feature allows passing an option called dataLayerName, which initializes a window variable that contains the dataLayer array and changes helper methods such as push(), trackEvent(), and trackView() to push data to this renamed dataLayer variable.

When fetching Tag Manager's script, this uses Google's recommendation to pass the dataLayerName as a query parameter.

Abitlity to rename dataLayerName in window.
@eljass eljass requested a review from Shinigami92 as a code owner February 2, 2024 13:26
@Shinigami92 Shinigami92 linked an issue Feb 7, 2024 that may be closed by this pull request
package-lock.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
src/data-layer-object.ts Outdated Show resolved Hide resolved
src/options.ts Show resolved Hide resolved
src/options.ts Outdated Show resolved Hide resolved
src/utils.ts Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
@eljass
Copy link
Contributor Author

eljass commented Feb 24, 2024

@Shinigami92 I refactored the code based on your reviews.

Those made sense, and I'm sorry for not noticing the use of pnpm and the fixes to take such a long time. The main issue I experienced with this refactor was the change in the global window declaration. We'll somehow need to handle the types for the window so we can use the dynamic key for the dataLayer name. The solution is to have an extended interface for the window, which is used only inside this module and not passed on to the end product. I am not sure if documentation for typing the window is in the scope of this module or change as if the end user is using typescript, they should handle the window's types by themselves.

Let me know if the changes make sense for you, as I did create a reusable function to get or construct the window.dataLayer array. Otherwise, there would be lot of repetition in defining window type to allow dynamic naiming of the dataLayer. I did also refactored tests to use dataLayer from instance, to avoid same repetition. I don't know if you had some other justifications to not do so previously?

@Shinigami92 Shinigami92 added the enhancement New feature or request label Feb 27, 2024
@Shinigami92 Shinigami92 self-requested a review February 27, 2024 19:51
@Shinigami92 Shinigami92 changed the title feat: add option to rename dataLayer variable Add option to use custom dataLayer variable Feb 27, 2024
@Shinigami92 Shinigami92 merged commit 989021b into gtm-support:main Feb 27, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow dynamic naming for dataLayer variable
2 participants