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

Send metrics as Google Tag Manager events #28

Merged

Conversation

SimenHansen
Copy link
Contributor

Most Googel Analytics implementations are in my experience working in the field done via Google Tag Manager. Added example for sending metrics via GTM, to then be picked up by Google Analytics or any other endpoint you may want to recieve data.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@SimenHansen
Copy link
Contributor Author

@googlebot I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

README.md Show resolved Hide resolved
// Assumes the global `dataLayer` array exists, see:
// https://developers.google.com/tag-manager/devguide
dataLayer.push({
event: 'web-vitals',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what this value is used for? Is it sent to Google Analytics or just used internally by GTM?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This key is what you build a trigger on in GTM. The value just have to match whatever you set in the trigger on the GTM side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sent anywhere unsless you want to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so then the you'd set up a trigger in your analytics tag in GTM to listen for the web-vitals event, and then configure how you want to send the data to Google Analytics?

Do the event_* properties automatically map, or would a user have to set that up themselves? (Sorry, never used GTM).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me give you a quick GTM workflow for setting this up:

  1. Create a GA Event Tag
    chrome_2020-05-10_23-01-28

  2. Map Event Tracking Parameters
    Here you need to manually map each "dataLayer"-variable seperate. So there is actually no automatic mapping here.
    bilde

  3. Define and set a trigger
    Here GTM reads from the event key defined at the top of the dataLayer.push()-object.
    chrome_2020-05-10_23-07-22

  4. Pull it all together
    chrome_2020-05-10_23-08-39

And it will look something like this. Every time a new dataLayer.push() is run, it will send an event to Google Analytics.

You can of course set more complex triggers, say if you only want to send the events on certain pages of your website or any other criteria that you might think of. I'm thinking of running this only on product-pages for a ecommerce client of mine as a start.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@madilk I'd recommend not sending these to GA as timing hits, as those are quite restricted (e.g. are sampled at 1%, can't be used in segments, etc.)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philipwalton Perhaps, it might be better to send as Events and Timing hits to give the flexibility later? Usually, we set the siteSpeedSampleRate field in GA hits to be much higher % than the default ~1%. Also, I wasn't sure about the timing segment and checked it out. Timing hit values can be used in custom segments within GA. Attaching a screenshot here.
timing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, maybe the sampling rules for timing hits has changed since I last tried them, but regardless the 1% sampling is the biggest blocker.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And to clarify, the 1% sampling rate is applied on both the client and the server, so changing the 1% to 100% will not actually get you 100%. See: https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings#sampling_considerations

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello @SimenHansen,
thanks a lot for the GTM workflow, it is very useful!
now that i can track the events in analytics, i'd like to go one step further, and get a quick visual repartition of "good"/"ok"/"poor" performances..
by any chance, would you have any idea of how this could be achieved using data studio?
let's say to produce graphs like the ones in CrUX Dashboard:
image

because having avg. values in analytics is better than nothing but does not help to get the full picture

README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
@SimenHansen SimenHansen requested a review from philipwalton May 10, 2020 19:09
@SimenHansen SimenHansen force-pushed the google-tag-manger-example branch from 8f8fa44 to 7357767 Compare May 10, 2020 20:03
README.md Outdated Show resolved Hide resolved
// Assumes the global `dataLayer` array exists, see:
// https://developers.google.com/tag-manager/devguide
dataLayer.push({
event: 'web-vitals',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so then the you'd set up a trigger in your analytics tag in GTM to listen for the web-vitals event, and then configure how you want to send the data to Google Analytics?

Do the event_* properties automatically map, or would a user have to set that up themselves? (Sorry, never used GTM).

// Assumes the global `dataLayer` array exists, see:
// https://developers.google.com/tag-manager/devguide
dataLayer.push({
event: 'web-vitals',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll merge this and then figure how where to link to in a separate PR.

@philipwalton philipwalton merged commit f2e6fcd into GoogleChrome:master May 10, 2020
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 this pull request may close these issues.

7 participants