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

Commit

Permalink
feat(Pixel): add more metrics (plt, sd, de, vp, sr)
Browse files Browse the repository at this point in the history
* screen colors
* document encoding
* viewport size
* screen resolution
* page load time
  • Loading branch information
CanRau committed Oct 6, 2018
1 parent 31f22ea commit ff4a428
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
22 changes: 21 additions & 1 deletion plugins/gatsby-plugin-pixel/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ const image = new Image()
// uid used to group visitors instead of PII
const uid = cuid()

const getDuration = () => {
const start = window.pixelStart || new Date()
const now = new Date()
const difference = now.getTime() - start.getTime()

if (difference === 0) {
return null
}

return difference
}

/**
* utm_source – Identifies which site sent the traffic - utm_source=Google
* utm_campaign - Identifies a specific product promotion or strategic campaign - utm_campaign=spring_sale
Expand All @@ -27,7 +39,15 @@ export const onRouteUpdate = (
setTimeout(() => {
// eslint-disable-next-line
const { title: dt, referrer } = document
const query = { uid, dt }
const query = {
uid,
dt,
plt: getDuration(),
sd: window.screen.colorDepth, // screen colors
de: document.characterSet, // document encoding
vp: `${window.innerWidth}x${window.innerHeight}`, // viewport size
sr: `${window.screen.width}x${window.screen.height}`, // screen resolution
}

if (referrer) query.dr = referrer // document referrer
if (utm_source) query.cs = utm_source // campaign source
Expand Down
9 changes: 9 additions & 0 deletions plugins/gatsby-plugin-pixel/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

export const onRenderBody = ({ setPostBodyComponents }) =>
setPostBodyComponents([
<script
key={'gatsby-plugin-pixel'}
dangerouslySetInnerHTML={{ __html: `window.pixelStart=new Date()` }}
/>,
])

0 comments on commit ff4a428

Please sign in to comment.