analytics icon indicating copy to clipboard operation
analytics copied to clipboard

not working if google-tag-manager initialized before via script

Open Stas-Buzunko opened this issue 4 years ago • 3 comments

I've developed a separate widget that can be installed on clients website. And issue appears if clients have their google-tag-manager initialised already via script with the same containerId

Here is an example


const analytics = Analytics({
	app: 'vr-crm',
	plugins: [
		googleTagManager({
			containerId
		})
	]
})

And use it like this

analytics.track(
  'dayClickEvent',
  {
	  value: dayStr
  },
  () => console.log('analytics called dayClickEvent on ', dayStr)
)

And it outputs callback but never fires the event. So i have 2 questions: 1st - is this expected behaviour? 2nd - is there any way to use already initialised google-tag-manager in Analytics?

And it works fine when i remove google-tag-manager

Stas-Buzunko avatar Feb 21 '21 16:02 Stas-Buzunko

googleTagManager pushs events to the dataLayer. See https://github.com/DavidWells/analytics/blob/master/packages/analytics-plugin-google-tag-manager/src/browser.js#L91

Debug/Verify your datalayer contains the fired events. See https://infotrust.com/articles/google-tag-manager-debugging-quick-tips/

Its up to the google tag manager rules you setup as to how those events pushed to the datalayer are handled. (Personally this is why I prefer normal GA over tag manager 😃)

DavidWells avatar Feb 23 '21 18:02 DavidWells

For anyone else looking at this, you might need to persist dataLayer when also initializing it through this package like so:

const analytics = Analytics({
	app: 'vr-crm',
	plugins: [
		googleTagManager({
			containerId,
			dataLayer: window?.dataLayer ?? []
		})
	]
})

Otherwise this package can and will reset the dataLayer

agustinv avatar Feb 13 '24 20:02 agustinv