feat(plugin): add support for Google Analytics 4 (gtag) #138
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
The deadline to switch to Google Analytics 4 is steadily approaching. After poking through this module and a few others (google-gtag-module, gtm-module), it seems like support has dried up. Since this library builds upon the battle-tested approach of
vue-analytics
, switching to usevue-gtag
is the least amount of lift to add support. This support burden is amplified by the recent release of Nuxt 3 and will require some slight tweaks to support the new module architecture.I think the best course of action is to start by adding gtag support as an opt-in option to this version of the plugin. This way, any consumers who want to bump their analytics scripts without updating Nuxt can do so. After that, the next major version of this plugin can be written to explicitly wrap the
vue-gtag
API only for Nuxt 3.I gave these changes a shakedown in local development and the new tag manager appears to be firing out events just fine:
Changes:
useGtag
(false by default) that allows users to switch to Google Analytics 4$ga
or$gtag
instances andvue-gtag
options ifuseGtag
is set to trueuseGtag
is setRelevant (open) issues:
#94
#111
#122
#131
#135
Temporary workarounds for other devs:
I've published a fork containing these exact changes to
@abbo/nuxt-google-analytics
and I'm currently using that fork in a published project here.