Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# gatsby-plugin-gdpr-cookies

# This is a temporary fork to see what kind of bugs are in this repository

Gatsby plugin to add Google Analytics (V4 is supported), Google Tag Manager, Facebook Pixel, TikTok Pixel and Hotjar in a GDPR form to your site.

**Version 2:** I did rewrite the whole plugin to remove all the ssr that was done before. This will ensure that no data is ever sent before the cookies are set to true and the plugin has been initialized. There are **no breaking changes**. The configuration is exactly the same and and `initializeAndTrack(location)` does still work as before. I also removed some dependencies to keep the bundle size as small as possible.
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "gatsby-plugin-gdpr-cookies",
"description": "Gatsby plugin to add google analytics, google tag manager facebook pixel, tik tok pixel and hotjar in a gdpr form to your site. Consent coming soon.",
"version": "2.0.9",
"author": "Andre Zimpel <[email protected]>",
"name": "gatsby-plugin-gdpr-cookies-klyngen",
"description": "Gatsby plugin to add google analytics, google tag manager facebook pixel, tik tok pixel and hotjar in a gdpr form to your site. Consent coming soon. Forked",
"version": "2.1.2",
"author": "Andre Zimpel <[email protected]>; Martin Klingenberg",
"bugs": {
"url": "https://github.com/andrezimpel/gatsby-plugin-gdpr-cookies/issues"
},
Expand All @@ -26,10 +26,8 @@
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.4",
"lodash": "^4.17.20",
"prettier": "^2.1.2"
},
"homepage": "https://github.com/andrezimpel/gatsby-plugin-gdpr-cookies#readme",
"keywords": [
"gatsby",
"gatsby-plugin",
Expand All @@ -54,7 +52,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/andrezimpel/gatsby-plugin-gdpr-cookies.git"
"url": "git+https://github.com/klyngen/gatsby-plugin-gdpr-cookies-klyngen.git"
},
"scripts": {
"build": "babel src --out-dir . --ignore **/__tests__",
Expand Down
4 changes: 2 additions & 2 deletions src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defaultOptions } from "./default-options"
import { initializeAndTrack } from './index'
import merge from "lodash/merge"

// init
export const onClientEntry = (_, pluginOptions = {}) => {
Expand Down Expand Up @@ -33,7 +32,8 @@ export const onClientEntry = (_, pluginOptions = {}) => {
}
}

const options = merge(defaultOptions, pluginOptions)
const options = { ...defaultOptions, ...pluginOptions }

window.gatsbyPluginGDPRCookiesOptions = options
}

Expand Down
53 changes: 31 additions & 22 deletions src/helper.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
exports.validGATrackingId = options =>
options.trackingId &&
options.trackingId.trim() !== ``
exports.validGATrackingId = (options) =>
options.trackingId && options.trackingId.trim() !== ``

exports.validGTMTrackingId = options =>
options.trackingId &&
options.trackingId.trim() !== ``
exports.validGTMTrackingId = (options) =>
options.trackingId && options.trackingId.trim() !== ``

exports.validFbPixelId = options =>
options.pixelId &&
options.pixelId.trim() !== ``
exports.validFbPixelId = (options) =>
options.pixelId && options.pixelId.trim() !== ``

exports.validTikTokPixelId = options =>
options.pixelId &&
options.pixelId.trim() !== ``
exports.validTikTokPixelId = (options) =>
options.pixelId && options.pixelId.trim() !== ``

exports.validHotjarId = options =>
exports.validHotjarId = (options) =>
options.hjid &&
options.hjid.trim() !== `` &&
options.hjsv &&
options.hjsv.trim() !== ``

exports.validChatwootConfig = options =>
exports.validChatwootConfig = (options) =>
options.baseUrl &&
options.baseUrl.trim() !== `` &&
options.websiteToken &&
options.websiteToken.trim() !== ``

exports.validLinkedinTrackingId = options =>
options.trackingId &&
options.trackingId.trim() !== ``

exports.getCookie = name => {
var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return v ? v[2] : null;
exports.validLinkedinTrackingId = (options) =>
options.trackingId && options.trackingId.trim() !== ``

/**
* @prop {string} name
* @returns {boolean} true if cooke exists and has value === true
* */
exports.isCookieEnabled = (name) => {
return document.cookie
.split(`;`)
.map((c) => c.trim())
.includes(`${name}=true`)
}

/**
* @param {string[]} environments*/
exports.isEnvironmentValid = (environments) => {
const currentEnvironment = process.env.ENV || process.env.NODE_ENV || `development`
let currentEnvironment = `production`
try {
currentEnvironment =
process?.env?.ENV || process?.env?.NODE_ENV || `development`
} catch {
console.warn(`Unable to parse environment. Lets think this is prod anyways`)
}
return environments.includes(currentEnvironment)
}
29 changes: 16 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@ const {
initializeAndTrackTikTokPixel,
initializeAndTrackHotjar,
initializeChatwoot,
initializeLinkedin
initializeLinkedin,
} = require('./services')

const { isEnvironmentValid } = require('./helper')

exports.initializeAndTrack = (location) => {
const options = window.gatsbyPluginGDPRCookiesOptions

if (isEnvironmentValid(options.environments)) {
if (location === undefined || location === null) {
console.error('Please provide a reach router location to the initializeAndTrack function.')
} else {
initializeAndTrackGoogleAnalytics(options.googleAnalytics, location)
initializeAndTrackGoogleTagManager(options.googleTagManager, location)
initializeAndTrackFacebookPixel(options.facebookPixel)
initializeAndTrackTikTokPixel(options.tikTokPixel)
initializeAndTrackHotjar(options.hotjar)
initializeChatwoot(options.chatwoot)
initializeLinkedin(options.linkedin)
}
if (!isEnvironmentValid(options.environments)) {
console.warn(`Tracking not activated as ${options.environments.join(', ')} does not match ${process?.env?.NODE_ENV}`);
}

if (location === undefined || location === null) {
console.error('Please provide a reach router location to the initializeAndTrack function.')
return;
}

initializeAndTrackGoogleAnalytics(options.googleAnalytics, location)
initializeAndTrackGoogleTagManager(options.googleTagManager, location)
initializeAndTrackFacebookPixel(options.facebookPixel)
initializeAndTrackTikTokPixel(options.tikTokPixel)
initializeAndTrackHotjar(options.hotjar)
initializeChatwoot(options.chatwoot)
initializeLinkedin(options.linkedin)
}
5 changes: 0 additions & 5 deletions src/services/chatwoot.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
const {
validChatwootConfig,
getCookie
} = require('../helper')

exports.addChatwoot = (options) => {
return new Promise((resolve, reject) => {
if (window.gatsbyPluginGDPRCookiesChatwootAdded) return resolve(true)
Expand Down
6 changes: 3 additions & 3 deletions src/services/facebook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
validFbPixelId,
getCookie
isCookieEnabled
} = require('../helper')

exports.addFacebookPixel = () => {
Expand Down Expand Up @@ -42,7 +42,7 @@ exports.addFacebookPixel = () => {
exports.initializeFacebookPixel = (options) => {
if (
!window.gatsbyPluginGDPRCookiesFacebookPixelInitialized &&
getCookie(options.cookieName) === `true` &&
isCookieEnabled(options.cookieName) &&
validFbPixelId(options)
) {
window.fbq(`init`, options.pixelId)
Expand All @@ -53,7 +53,7 @@ exports.initializeFacebookPixel = (options) => {

exports.trackFacebookPixel = (options) => {
if (
getCookie(options.cookieName) === `true` &&
isCookieEnabled(options.cookieName) &&
validFbPixelId(options) &&
typeof window.fbq === "function"
) {
Expand Down
6 changes: 3 additions & 3 deletions src/services/google-analytics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
validGATrackingId,
getCookie
isCookieEnabled
} = require('../helper')

exports.addGoogleAnalytics = ({ trackingId }) => {
Expand All @@ -23,7 +23,7 @@ exports.addGoogleAnalytics = ({ trackingId }) => {
exports.initializeGoogleAnalytics = (options) => {
if (
!window.gatsbyPluginGDPRCookiesGoogleAnalyticsInitialized &&
getCookie(options.cookieName) === `true` &&
isCookieEnabled(options.cookieName) &&
validGATrackingId(options)
) {
window.dataLayer = window.dataLayer || [];
Expand All @@ -46,7 +46,7 @@ exports.initializeGoogleAnalytics = (options) => {

exports.trackGoogleAnalytics = (options, location) => {
if (
getCookie(options.cookieName) === `true` &&
isCookieEnabled(options.cookieName) &&
validGATrackingId(options) &&
typeof window.gtag === "function"
) {
Expand Down
6 changes: 3 additions & 3 deletions src/services/google-tag-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { validGTMTrackingId, getCookie } = require(`../helper`)
const { validGTMTrackingId, isCookieEnabled } = require(`../helper`)

exports.addGoogleTagManager = (
{ trackingId, dataLayerName },
Expand Down Expand Up @@ -47,7 +47,7 @@ exports.initializeGoogleTagManager = (options) => {
// console.log(`initing tag manager`)
if (
!window.gatsbyPluginGDPRCookiesGoogleTagManagerInitialized &&
getCookie(options.cookieName) === `true` &&
isCookieEnabled(options.cookieName) &&
validGTMTrackingId(options)
) {
window.dataLayer = window.dataLayer || []
Expand All @@ -72,7 +72,7 @@ exports.initializeGoogleTagManager = (options) => {
exports.trackGoogleTagManager = (options, location) => {
// console.log(`tracking tag manager`)
if (
getCookie(options.cookieName) === `true` &&
isCookieEnabled(options.cookieName) &&
validGTMTrackingId(options) &&
typeof window.gtag === `function`
) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/hotjar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
validHotjarId,
getCookie
isCookieEnabled
} = require('../helper')

exports.addHotjar = (options) => {
Expand All @@ -27,7 +27,7 @@ exports.addHotjar = (options) => {
exports.initializeHotjar = (options) => {
if (
!window.gatsbyPluginGDPRCookiesHotjarInitialized &&
getCookie(options.cookieName) === `true` &&
isCookieEnabled(options.cookieName) &&
validHotjarId(options)
) {
window.gatsbyPluginGDPRCookiesHotjarInitialized = true
Expand Down
Loading