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

Allow dynamic naming for dataLayer variable #402

Closed
eljass opened this issue Feb 2, 2024 · 1 comment · Fixed by #401
Closed

Allow dynamic naming for dataLayer variable #402

eljass opened this issue Feb 2, 2024 · 1 comment · Fixed by #401

Comments

@eljass
Copy link
Contributor

eljass commented Feb 2, 2024

Request / Idea

The Google Tag Manager (GTM) allows for the customization of the dataLayer variable name through its configuration. This feature is particularly useful for websites that need to run multiple instances of GTM or wish to avoid naming conflicts with existing JavaScript variables. Currently, the gtm-support module assumes the use of the default dataLayer variable name, which limits its flexibility and applicability in environments where custom dataLayer names are used or multiple GTM containers are deployed.

Proposed Solution

I propose enhancing the gtm-support module to support dynamic naming of the dataLayer variable. This could be achieved by allowing users to specify the name of the dataLayer variable when initializing the module. The module would then use this custom name in all operations related to the data layer, including pushing events and variables.

API Changes

The change could involve updating the module's initialization function to accept an additional configuration option, such as dataLayerName. For example:

interface GtmSupportOptions {
  ...
  /**
   * The name of the dataLayer variable to push events.
   *
   * @example dataLayerName: 'dataLayerDemo'
   *
   * @default dataLayer
   */
  dataLayerName?: string;
}

class GtmSupport {
  constructor(options: GtmSupportOptions) {
    ...
    this.options = {
      ...
      dataLayerName: 'dataLayer',
      ...options,
    };
  }
  // Further implementation...
}

Benefits

  • Flexibility: Users can integrate gtm-support more seamlessly into projects with existing naming conventions or multiple GTM instances.
  • Compatibility: Increases the module's compatibility with complex web applications and marketing strategies that require the use of multiple data layers.
  • Customization: Provides users with greater control over their GTM implementation, aligning with the customizable nature of GTM itself.

Use Cases

  • Websites that utilize multiple GTM containers and need to differentiate between different dataLayer variables.
  • Projects that already have a global dataLayer variable used for purposes other than GTM and need to integrate GTM without naming conflicts.
  • Users seeking to adopt naming conventions that align more closely with their project's terminology or organizational practices.

Implementation Considerations

  • The module should ensure backward compatibility, defaulting to the standard dataLayer name when the custom name is not provided.
  • Care should be taken to validate the custom dataLayer name to ensure it adheres to JavaScript variable naming conventions and does not conflict with existing global variables.

Pull request

#401

@Shinigami92 Shinigami92 linked a pull request Feb 7, 2024 that will close this issue
@Shinigami92
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants