Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Dynamics Marketing #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ module.exports = {
hjsv: 'YOUR_HOTJAR_SNIPPET_VERSION',
cookieName: 'gatsby-gdpr-hotjar', // default
},
dynamicsMarketing: {
id: "YOUR_ID_FROM_DYNAMICS_MARKETING", //leave empty if you want to disable the tracker
script: "YOUR_DYNAMICS_MARKETING_TRACKING_SCRIPT",
formScript: "YOUR_DYNAMICS_MARKETING_FORM_SCRIPT",
className: "d365-mkt-config", // default
websiteId: "YOUR_DYNAMICS_MARKETING_DATA_WEBSITE_ID",
dataHostName: "YOUR_DYNAMICS_MARKETING_DATA_HOSTNAME",
cookieName: "gatsby-gdpr-dynamics-marketing", // default
}
// defines the environments where the tracking should be available - default is ["production"]
environments: ['production', 'development']
},
Expand Down Expand Up @@ -182,6 +191,49 @@ Stands for 'Hotjar Snippet Version' - The version of the Tracking Code you are u

You can use a custom cookie name if you need to!

### Dynamics Marketing

Add the following div tags to all pages that are to be tracked
Replace values with parameters from gatsby.config file:

```
<div id = ""></div>
<div
className = "d365-mkt-config hidden"
data-website-id = ""
data-hostname = ""
></div>
```

#### `id`

Your Marketing site's ID.

#### `script`

Your tracking script from dynamics marketing.

#### `formScript`

Your script for Marketing forms.

#### `className`

Default is 'd365-mkt-config hidden'. Div class

#### `websiteId`

Your Marketing data-website-id.

#### `dataHostName`

Your Marketing data-hostName.

#### `cookieName`

You can use a custom cookie name if you need to!


## Initialize and track
This gatsby plugin now supports initializing and tracking right after a user accepts the cookie consent.

Expand Down
4 changes: 4 additions & 0 deletions src/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ export const defaultOptions = {
},
hotjar: {
cookieName: `gatsby-gdpr-hotjar`
},
dynamicsMarketing: {
cookieName: `gatsby-gdpr-dynamics-marketing`,
className: `d365-mkt-config hidden`
}
}
2 changes: 2 additions & 0 deletions src/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export const onClientEntry = (_, pluginOptions = {}) => {
window.gatsbyPluginGDPRCookiesFacebookPixelAdded = false
window.gatsbyPluginGDPRCookiesTikTokPixelAdded = false
window.gatsbyPluginGDPRCookiesHotjarAdded = false
window.gatsbyPluginGDPRCookiesDynamicsMarketingAdded = false

window.gatsbyPluginGDPRCookiesGoogleAnalyticsInitialized = false
window.gatsbyPluginGDPRCookiesGoogleTagManagerInitialized = false
window.gatsbyPluginGDPRCookiesFacebookPixelInitialized = false
window.gatsbyPluginGDPRCookiesTikTokPixelInitialized = false
window.gatsbyPluginGDPRCookiesHotjarInitialized = false
window.gatsbyPluginGDPRCookiesDynamicsMarketingInitialized = false

// google tag manager setup
const { googleTagManager } = pluginOptions
Expand Down
6 changes: 6 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ exports.validHotjarId = options =>
options.hjsv &&
options.hjsv.trim() !== ``

exports.validDMId = options =>
options.id &&
options.id.trim() !== `` &&
options.websiteId &&
options.websiteId.trim() !== ``

exports.getCookie = name => {
var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return v ? v[2] : null;
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const {
initializeAndTrackGoogleTagManager,
initializeAndTrackFacebookPixel,
initializeAndTrackTikTokPixel,
initializeAndTrackHotjar
initializeAndTrackHotjar,
initializeAndTrackDynamicsMarketing
} = require('./services')

const { isEnvironmentValid } = require('./helper')
Expand All @@ -20,6 +21,7 @@ exports.initializeAndTrack = (location) => {
initializeAndTrackFacebookPixel(options.facebookPixel)
initializeAndTrackTikTokPixel(options.tikTokPixel)
initializeAndTrackHotjar(options.hotjar)
initializeAndTrackDynamicsMarketing(options.dynamicsMarketing)
}
}
}
48 changes: 48 additions & 0 deletions src/services/dynamics-marketing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const {
validDMId,
getCookie
} = require("../helper")

exports.addDynamicsMarketing = ( options ) => {
return new Promise((resolve, reject) => {
if (window.gatsbyPluginGDPRCookiesDynamicsMarketingAdded)
return resolve(true)

/* eslint disable */
const head = document.getElementsByTagName('head')[0]
const script = document.createElement(`script`)
script.type = `text/javascript`
script.src = options.script
head.appendChild(script);

const formScript = document.createElement(`script`)
formScript.type = `text/javascript`
formScript.src = options.formScript
head.appendChild(formScript);

(
window,
document,
'script',
options.script
)

/* eslint-enable */

window.gatsbyPluginGDPRCookiesDynamicsMarketingAdded = true
resolve(true)
});
}

exports.initializeDynamicsMarketing = (options) => {
if (
!window.gatsbyPluginGDPRCookiesDynamicsMarketingInitialized &&
getCookie(options.cookieName) === `true` &&
validDMId(options)
) {
window.gatsbyPluginGDPRCookiesDynamicsMarketingInitialized = true
}
}
exports.trackDynamicsMarketing = (options) => {
// --
}
21 changes: 21 additions & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
validFbPixelId,
validTikTokPixelId,
validHotjarId,
validDMId,
getCookie
} = require('../helper')

Expand Down Expand Up @@ -37,6 +38,12 @@ const {
trackHotjar
} = require('./hotjar')

const {
addDynamicsMarketing,
initializeDynamicsMarketing,
trackDynamicsMarketing
} = require('./dynamics-marketing')

exports.initializeAndTrackGoogleAnalytics = (options, location) => {
if (
getCookie(options.cookieName) === `true` &&
Expand Down Expand Up @@ -111,3 +118,17 @@ exports.initializeAndTrackHotjar = (options) => {
})
}
}

exports.initializeAndTrackDynamicsMarketing = (options) => {
if(
getCookie(options.cookieName) === `true` &&
validDMId(options)
) {
addDynamicsMarketing(options).then((status) => {
if(status) {
initializeDynamicsMarketing(options)
trackDynamicsMarketing(options)
}
})
}
}