Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
feat(Pixel): use Beacon API where available
Browse files Browse the repository at this point in the history
  • Loading branch information
CanRau committed Oct 6, 2018
1 parent 4b6acd8 commit 11d0455
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions plugins/gatsby-plugin-pixel/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { parse, stringify } = require(`@gaiama/query-string`)
// store views so we won't count recurring pages
const views = {}
// init Image here to be reused on every route change
const pixel = new Image()
const image = new Image()
// uid used to group visitors instead of PII
const uid = cuid()

Expand Down Expand Up @@ -34,8 +34,15 @@ exports.onRouteUpdate = (
if (utm_campaign) query.cn = utm_campaign // campaign name
if (utm_content) query.cc = utm_content // campaign content
if (version) query.av = version // application version
if (window.navigator.sendBeacon) query.beacon = 1

pixel.src = stringify(query, endpoint)
const pixel = stringify(query, endpoint)

if (window.navigator.sendBeacon) {
window.navigator.sendBeacon(pixel)
} else {
image.src = pixel
}
}, 1000)
}
}

0 comments on commit 11d0455

Please sign in to comment.