Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
gatsby-plugin-google-gtag-optin
Advanced tools
Changelog
Readme
This is a fork of the official Gatsby plugin gatsby-plugin-google-gtag. It's supposed to be used as a drop in replacement.
It functions the same but ONLY after the user has consented. You can signal consent by setting a localStorage optinKey (default is gtag_optin
) to true
and calling window.loadGtag()
to immediately load the Google Global Site Tag. If you set the key in localStorage you don't have to call window.loadGtag()
on subsequent visits.
Example:
import { GTAG_OPTIN_KEY } from "gatsby-plugin-google-gtag-optin" // Or use your own if you changed it in the config
const consent = () => {
localStorage.setItem(GTAG_OPTIN_KEY, true)
if (typeof window.loadGtag == "function") {
window.loadGtag()
}
}
Easily add Google Global Site Tag to your Gatsby site.
The global site tag (gtag.js) is a JavaScript tagging framework and API that allows you to send event data to Google Analytics, Google Ads, Campaign Manager, Display & Video 360, and Search Ads 360.
Global site tag (gtag.js) is meant to combine multiple Google tagging systems and can replace older ones such as analytics.js (gatsby-plugin-google-analytics).
For more general information on gtag you can read Google's official documentation on the subject: https://developers.google.com/gtagjs/.
If you're migrating from analytics.js (gatsby-plugin-google-analytics) you can read about the subtle API differences in more depth at: https://developers.google.com/analytics/devguides/collection/gtagjs/migration.
NOTE: This plugin only works in production mode! To test your Global Site Tag is installed and firing events correctly run: gatsby build && gatsby serve.
npm install gatsby-plugin-google-gtag
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-google-gtag`,
options: {
// You can add multiple tracking ids and a pageview event will be fired for all of them.
trackingIds: [
"GA-TRACKING_ID", // Google Analytics / GA
"AW-CONVERSION_ID", // Google Ads / Adwords / AW
"DC-FLOODIGHT_ID", // Marketing Platform advertising products (Display & Video 360, Search Ads 360, and Campaign Manager)
],
// This object gets passed directly to the gtag config command
// This config will be shared across all trackingIds
gtagConfig: {
optimize_id: "OPT_CONTAINER_ID",
anonymize_ip: true,
cookie_expires: 0,
},
// This object is used for configuration specific to this plugin
pluginConfig: {
// Puts tracking script in the head instead of the body
head: false,
// Setting this parameter is also optional
respectDNT: true,
// Avoids sending pageview hits from custom paths
exclude: ["/preview/**", "/do-not-track/me/too/"],
// localStorage key that has to be set to true for plugin to load
optinKey: "gtag_optin", // default
},
},
},
],
}
This plugin automatically sends a "pageview" event to all products given as "trackingIds" on every Gatsbys route change.
If you want to call a custom event you have access to window.gtag
where you can call an event for all products:
window.gtag("event", "click", { ...data })
or you can target a specific product:
window.gtag("event", "click", { send_to: "AW-CONVERSION_ID", ...data })
In either case don't forget to guard against SSR:
typeof window !== "undefined" && window.gtag("event", "click", { ...data })
<OutboundLink>
componentTo make it easy to track clicks on outbound links the plugin provides a component.
To use it, simply import it and use it like you would the <a>
element e.g.
import React from "react"
import { OutboundLink } from "gatsby-plugin-google-gtag"
export default () => (
<div>
<OutboundLink href="https://www.gatsbyjs.org/packages/gatsby-plugin-google-gtag/">
Visit the Google Global Site Tag plugin page!
</OutboundLink>
</div>
)
Some countries (such as Germany) require you to use the _anonymizeIP function for Google Site Tag. Otherwise you are not allowed to use it. The option adds the block of code below:
function gaOptout() {
;(document.cookie =
disableStr + "=true; expires=Thu, 31 Dec 2099 23:59:59 UTC;path=/"),
(window[disableStr] = !0)
}
var gaProperty = "UA-XXXXXXXX-X",
disableStr = "ga-disable-" + gaProperty
document.cookie.indexOf(disableStr + "=true") > -1 && (window[disableStr] = !0)
If your visitors should be able to set an Opt-Out-Cookie (No future tracking) you can set a link e.g. in your imprint as follows:
<a href="javascript:gaOptout();">Deactivate Google Tracking</a>
If you need to use Google Optimize for A/B testing, you can add this optional Optimize container id to allow Google Optimize to load the correct test parameters for your site.
The gtagConfig
is passed directly to the gtag config command, so you can specify everything it supports, e.g. gtagConfig.cookie_name
, gtagConfig.sample_rate
. If you're migrating from the analytics.js plugin, this means that all Create Only Fields should be snake_cased.
If you enable this optional option, Google Global Site Tag will not be loaded at all for visitors that have "Do Not Track" enabled. While using Google Global Site Tag does not necessarily constitute Tracking, you might still want to do this to cater to more privacy oriented users.
If you need to exclude any path from the tracking system, you can add it (one or more) to this optional array as glob expressions.
FAQs
Gatsby plugin to add google gtag onto a site
The npm package gatsby-plugin-google-gtag-optin receives a total of 42 weekly downloads. As such, gatsby-plugin-google-gtag-optin popularity was classified as not popular.
We found that gatsby-plugin-google-gtag-optin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.