Skip to content

Commit

Permalink
fix(gatsby-plugin-google-gtag): OutboundLink Forward Ref (#22705)
Browse files Browse the repository at this point in the history
* gatsby-plugin-google-gtag OutboundLink Forward Ref

update gatsby-plugin-google-gtag OutboundLink component to forward ref onto DOM element.

* fix typo

* linting fix

* remove semi clons
  • Loading branch information
cbravo authored Apr 1, 2020
1 parent d5894c1 commit 7a4db4b
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions packages/gatsby-plugin-google-gtag/src/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import React from "react"
import PropTypes from "prop-types"

function OutboundLink(props) {
return (
<a
{...props}
onClick={e => {
if (typeof props.onClick === `function`) {
props.onClick(e)
}
let redirect = true
if (
e.button !== 0 ||
e.altKey ||
e.ctrlKey ||
e.metaKey ||
e.shiftKey ||
e.defaultPrevented
) {
redirect = false
}
if (props.target && props.target.toLowerCase() !== `_self`) {
redirect = false
}
if (window.gtag) {
window.gtag(`event`, `click`, {
event_category: `outbound`,
event_label: props.href,
transport_type: redirect ? `beacon` : ``,
event_callback: function() {
if (redirect) {
document.location = props.href
}
},
})
} else {
if (redirect) {
document.location = props.href
}
const OutboundLink = React.forwardRef(({ children, ...props }, ref) => (
<a
ref={ref}
{...props}
onClick={e => {
if (typeof props.onClick === `function`) {
props.onClick(e)
}
let redirect = true
if (
e.button !== 0 ||
e.altKey ||
e.ctrlKey ||
e.metaKey ||
e.shiftKey ||
e.defaultPrevented
) {
redirect = false
}
if (props.target && props.target.toLowerCase() !== `_self`) {
redirect = false
}
if (window.gtag) {
window.gtag(`event`, `click`, {
event_category: `outbound`,
event_label: props.href,
transport_type: redirect ? `beacon` : ``,
event_callback: function() {
if (redirect) {
document.location = props.href
}
},
})
} else {
if (redirect) {
document.location = props.href
}
}

return false
}}
/>
)
}
return false
}}
>
{children}
</a>
))

OutboundLink.propTypes = {
href: PropTypes.string,
Expand Down

0 comments on commit 7a4db4b

Please sign in to comment.