This is a Gatsby Remark plugin that provides the same tracking that gatsby-plugin-google-analytics
' (or gatsby-plugin-google-gtag
)s OutboundLink
and trackCustomEvent
accomplishes within your actual codebase: the ability to track link clicks to internal & external locations within your markdown. It provides an onClick
handler to all links that come from Markdown in your site.
With npm:
npm install --save gatsby-remark-google-analytics-track-links
or with yarn:
yarn add gatsby-remark-google-analytics-track-links
// In gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
//...
`gatsby-remark-google-analytics-track-links`,
],
},
},
];
This plugin supports both the (now) legacy window.ga
tag, and the newer gtag
. If both tags exist on your site, events will be sent to both tags (with the assumption being that most sites will only have one version of the tag).
This plugin adds target="_blank" rel="noopener nofollow noreferrer"
properties by default to your external links, as well as a targetable class name ("siteLink"
by default). You can opt-out of adding the rel
attribute, if desired. Here are all the defaults:
{
allowFollowLinks: false, //set to true to remove the "rel" attribute entirely
className: "siteLink",
localLinkMatch: false, //otherwise should be a string / regex
gaOptions: {
internalLinkTitle: "Internal Link", //fallback for internal links if event category not passed
externalLinkTitle: "External Link", //fallback for external links if event category not passed
eventCategory: false, //pass a string to customize
eventAction: `click`,
eventLabel: false //pass a string to customize
},
rel: "noopener nofollow noreferrer", //added to each anchor tag
runInDev: false, //update to true if your version of GA is available in dev and you want to test tracking
target: "_blank" // added to each anchor tag
}
You can customize any of the options you would like; the others will be copied from the defaults:
// In gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
//...
{
resolve: "gatsby-remark-google-analytics-track-links",
options: {
target: "someValue",
rel: "noopener noreferrer",
className: "theClassIdLikeInstead",
},
},
],
},
},
];
allowFollowLinks
: removes therel
attribute entirely, allowing you to take advantage of "follow" linksclassName
: the class added to each anchor taglocalLinkMatch
: string or regex if you need additional matching options for catching internal links; I think most people end up using a combination ofgatsby-remark-relative-links
andgatsby-plugin-catch-links
, but if you needed to catch "absolute-path-y" links in your Markdown that are in fact local links, then this option will let you properly flag the links as internal ones for your GA eventsgaOptions.internalLinkTitle
: if you aren't going to use your own custom event category this is the category that will be used for internal linksgaOptions.externalLinkTitle
: same but for external linksgaOptions.eventCategory
: supply this string to override the choices for internal / external link titlesgaOptions.eventAction
: "click" by default, or your own stringgaOptions.eventLabel
: if you don't supply your own string, defaults toevent.currentTarget.href
; the URL of the link that was clicked, in other wordsrel
: defines the relationship from the current page to the linked page. Defaults tonoopener nofollow noreferrer
for external links; is not added to internal linksrunInDev
: by default, this plugin does not run while in Dev mode; this mirrors the settings forgatsby-plugin-google-analytics
. If you are installing the GA tag on your own and want to test event sending in development, you can set this flag totrue
to test. Otherwise, you can test by runninggatsby build
and thengatsby serve
target
: where to open the link. Defaults to_blank
for external links; is not added for internal links
Contributions are welcome, however I would ask that all feature requests come with additional tests added. You are also welcome to contribute in the way of adding tests, though heavy test refactors may be turned away unless there is a pressing need for said refactor. Additionally, I would ask that all PR's be submitted with the default prettier
formatting.
Thanks to:
- Gabriel Cutrini for contributing to 0.0.6 by fixing links with inline content!