Skip to content

Commit a189e14

Browse files
committed
Fix link errors + handle other types of clicks
1 parent 2d13c88 commit a189e14

File tree

6 files changed

+53
-24
lines changed

6 files changed

+53
-24
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
}
1919
},
2020
"env": {
21+
"browser": true,
2122
"es6": true,
2223
"node": true,
2324
"jest": true
Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
import React from "react"
2+
import PropTypes from "prop-types"
23

3-
export function OutBoundLink(props) {
4+
function OutboundLink(props) {
45
return (
56
<a
67
{...props}
78
onClick={e => {
8-
ga("send", "event", "outbound", "click", props.href, {
9-
transport: "beacon",
10-
hitCallback: function() {
9+
let redirect = true
10+
if (
11+
e.button !== 0 ||
12+
e.altKey ||
13+
e.ctrlKey ||
14+
e.metaKey ||
15+
e.shiftKey ||
16+
e.defaultPrevented
17+
) {
18+
redirect = false
19+
}
20+
21+
if (window.ga) {
22+
window.ga(`send`, `event`, `outbound`, `click`, props.href, {
23+
transport: `beacon`,
24+
hitCallback: function() {
25+
if (redirect) {
26+
document.location = props.href
27+
}
28+
},
29+
})
30+
} else {
31+
if (redirect) {
1132
document.location = props.href
12-
},
13-
})
33+
}
34+
}
35+
1436
return false
1537
}}
1638
/>
1739
)
1840
}
41+
42+
OutboundLink.propTypes = {
43+
href: PropTypes.string,
44+
}
45+
46+
export { OutboundLink }

www/src/components/navigation.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react"
22
import Link from "gatsby-link"
33
import TwitterIcon from "react-icons/lib/fa/twitter"
44
import GithubIcon from "react-icons/lib/go/mark-github"
5-
import { OutBoundLink } from "gatsby-plugin-google-analytics"
5+
import { OutboundLink } from "gatsby-plugin-google-analytics"
66

77
import SearchForm from "../components/search-form"
88
import DiscordIcon from "../components/discord"
@@ -169,7 +169,7 @@ export default ({ pathname }) => {
169169
iconStyles={{ ...socialIconsStyles }}
170170
isHomepage={isHomepage}
171171
/>
172-
<OutBoundLink
172+
<OutboundLink
173173
href="https://github.com/gatsbyjs/gatsby"
174174
title="GitHub"
175175
css={{
@@ -178,7 +178,7 @@ export default ({ pathname }) => {
178178
}}
179179
>
180180
<GithubIcon style={{ verticalAlign: `text-top` }} />
181-
</OutBoundLink>
181+
</OutboundLink>
182182

183183
<div
184184
css={{
@@ -187,7 +187,7 @@ export default ({ pathname }) => {
187187
[presets.Hd]: { display: `inline-block` },
188188
}}
189189
>
190-
<OutBoundLink
190+
<OutboundLink
191191
href="https://discord.gg/0ZcbPKXt5bVoxkfV"
192192
title="Discord"
193193
css={{
@@ -196,8 +196,8 @@ export default ({ pathname }) => {
196196
}}
197197
>
198198
<DiscordIcon overrideCSS={{ verticalAlign: `text-top` }} />
199-
</OutBoundLink>
200-
<OutBoundLink
199+
</OutboundLink>
200+
<OutboundLink
201201
href="https://twitter.com/gatsbyjs"
202202
title="@gatsbyjs"
203203
css={{
@@ -207,7 +207,7 @@ export default ({ pathname }) => {
207207
}}
208208
>
209209
<TwitterIcon style={{ verticalAlign: `text-top` }} />
210-
</OutBoundLink>
210+
</OutboundLink>
211211
</div>
212212
</div>
213213
</div>

www/src/pages/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react"
22
import Link from "gatsby-link"
3-
import { OutBoundLink } from "gatsby-plugin-google-analytics"
3+
import { OutboundLink } from "gatsby-plugin-google-analytics"
44

55
import presets, { colors } from "../utils/presets"
66
import { rhythm, scale, options } from "../utils/typography"
@@ -83,9 +83,9 @@ class IndexRoute extends React.Component {
8383
<FuturaParagraph>
8484
Don't build a website with last decade's tech. The future of
8585
the web is mobile, JavaScript and APIs—the {` `}
86-
<OutBoundLink href="https://jamstack.org/">
86+
<OutboundLink href="https://jamstack.org/">
8787
JAMstack
88-
</OutBoundLink>. Every website is a web app and every web app
88+
</OutboundLink>. Every website is a web app and every web app
8989
is a website. Gatsby.js is the universal JavaScript framework
9090
you’ve been waiting for.
9191
</FuturaParagraph>

www/src/templates/template-blog-post.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Link from "gatsby-link"
44
import ArrowForwardIcon from "react-icons/lib/md/arrow-forward"
55
import ArrowBackIcon from "react-icons/lib/md/arrow-back"
66
import Img from "gatsby-image"
7-
import { OutBoundLink } from "gatsby-plugin-google-analytics"
7+
import { OutboundLink } from "gatsby-plugin-google-analytics"
88

99
import presets, { colors } from "../utils/presets"
1010
import typography, { rhythm, scale, options } from "../utils/typography"
@@ -161,9 +161,9 @@ class BlogPostTemplate extends React.Component {
161161
<span>
162162
{` `}
163163
(originally published at{` `}
164-
<OutBoundLink href={post.frontmatter.canonicalLink}>
164+
<OutboundLink href={post.frontmatter.canonicalLink}>
165165
{post.frontmatter.publishedAt}
166-
</OutBoundLink>)
166+
</OutboundLink>)
167167
</span>
168168
)}
169169
</BioLine>
@@ -191,9 +191,9 @@ class BlogPostTemplate extends React.Component {
191191
post.frontmatter.imageAuthorLink && (
192192
<em>
193193
Image by{` `}
194-
<OutBoundLink href={post.frontmatter.imageAuthorLink}>
194+
<OutboundLink href={post.frontmatter.imageAuthorLink}>
195195
{post.frontmatter.imageAuthor}
196-
</OutBoundLink>
196+
</OutboundLink>
197197
</em>
198198
)}
199199
</div>

www/src/templates/template-contributor-page.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react"
22
import Img from "gatsby-image"
3-
import { OutBoundLink } from "gatsby-plugin-google-analytics"
3+
import { OutboundLink } from "gatsby-plugin-google-analytics"
44

55
import Container from "../components/container"
66
import BlogPostPreviewItem from "../components/blog-post-preview-item"
@@ -46,10 +46,10 @@ class ContributorPageTemplate extends React.Component {
4646
>
4747
{contributor.bio}
4848
</p>
49-
<OutBoundLink href={`https://twitter.com/${contributor.twitter}`}>
49+
<OutboundLink href={`https://twitter.com/${contributor.twitter}`}>
5050
{` `}
5151
{contributor.twitter}
52-
</OutBoundLink>
52+
</OutboundLink>
5353
</div>
5454
</div>
5555
<div

0 commit comments

Comments
 (0)