mib200 / vue-gtm

Simple implementation of Google Tag Manager in Vue.js 2.0
Apache License 2.0
243 stars 85 forks source link

Bug: "event" should be optional in DataLayerObject #119

Closed garyo closed 3 years ago

garyo commented 3 years ago

Info

Tool Version
Plugin v3.4.0-vue2
Vue v2.x.x
Node v12
OS linux

Input

If you try to push a plain userId to the data layer, you get a Typescript error that "event" is required:

    if (Vue.gtm.enabled()) {
      window.dataLayer?.push({userId: user?.uid || ''})
    }

Output or Error

Object of type ... is not assignable to parameter of type 'DataLayerObject'. Property 'event' is missing... but is required in type 'DataLayerObject'.

Additional Context

I'm not an expert on this, but a simple fix that works for me is to make event optional at line 8 of plugin.d.ts, like this:

export interface DataLayerObject extends Record<string, any> {
    event?: string;
}