forked from openedx/frontend-platform
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add GoogleAnalytics 4 to frontend-platform
PR openedx#472 --------- Co-authored-by: Glib Glugovskiy <[email protected]>
- Loading branch information
1 parent
299ad79
commit c174b29
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @implements {GoogleAnalyticsLoader} | ||
* @memberof module:GoogleAnalytics | ||
*/ | ||
class GoogleAnalyticsLoader { | ||
constructor({ config }) { | ||
this.analyticsId = config.GOOGLE_ANALYTICS_4_ID; | ||
} | ||
|
||
loadScript() { | ||
if (!this.analyticsId) { | ||
return; | ||
} | ||
|
||
global.googleAnalytics = global.googleAnalytics || []; | ||
const { googleAnalytics } = global; | ||
|
||
// If the snippet was invoked do nothing. | ||
if (googleAnalytics.invoked) { | ||
return; | ||
} | ||
|
||
// Invoked flag, to make sure the snippet | ||
// is never invoked twice. | ||
googleAnalytics.invoked = true; | ||
|
||
googleAnalytics.load = (key, options) => { | ||
const scriptSrc = document.createElement('script'); | ||
scriptSrc.type = 'text/javascript'; | ||
scriptSrc.async = true; | ||
scriptSrc.src = `https://www.googletagmanager.com/gtag/js?id=${key}`; | ||
|
||
const scriptGtag = document.createElement('script'); | ||
scriptGtag.innerHTML = ` | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${key}'); | ||
`; | ||
|
||
// Insert our scripts next to the first script element. | ||
const first = document.getElementsByTagName('script')[0]; | ||
first.parentNode.insertBefore(scriptSrc, first); | ||
first.parentNode.insertBefore(scriptGtag, first); | ||
googleAnalytics._loadOptions = options; // eslint-disable-line no-underscore-dangle | ||
}; | ||
|
||
// Load GoogleAnalytics with your key. | ||
googleAnalytics.load(this.analyticsId); | ||
} | ||
} | ||
|
||
export default GoogleAnalyticsLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { GoogleAnalyticsLoader } from './index'; | ||
|
||
const googleAnalyticsId = 'test-key'; | ||
|
||
describe('GoogleAnalytics', () => { | ||
let body; | ||
let gaScriptSrc; | ||
let gaScriptGtag; | ||
let data; | ||
|
||
beforeEach(() => { | ||
window.googleAnalytics = []; | ||
}); | ||
|
||
function loadGoogleAnalytics(scriptData) { | ||
const script = new GoogleAnalyticsLoader(scriptData); | ||
script.loadScript(); | ||
} | ||
|
||
describe('with valid GOOGLE_ANALYTICS_4_ID', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = '<script id="stub" />'; | ||
data = { | ||
config: { | ||
GOOGLE_ANALYTICS_4_ID: googleAnalyticsId, | ||
}, | ||
}; | ||
loadGoogleAnalytics(data); | ||
expect(global.googleAnalytics.invoked).toBe(true); | ||
body = document.body.innerHTML; | ||
gaScriptSrc = `https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsId}`; | ||
gaScriptGtag = `gtag('config', '${googleAnalyticsId}');`; | ||
}); | ||
|
||
it('should initialize google analytics', () => { | ||
expect(body).toMatch(gaScriptSrc); | ||
expect(body).toMatch(gaScriptGtag); | ||
}); | ||
|
||
it('should not invoke snippet twice', () => { | ||
loadGoogleAnalytics(data); | ||
|
||
expect(global.googleAnalytics.invoked).toBe(true); | ||
|
||
expect(body).toMatch(gaScriptSrc); | ||
expect(body).toMatch(gaScriptGtag); | ||
|
||
let count = (body.match(new RegExp(gaScriptSrc.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) || []).length; | ||
expect(count).toBe(1); | ||
|
||
count = (body.match(new RegExp(gaScriptGtag.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) || []).length; | ||
expect(count).toBe(1); | ||
}); | ||
}); | ||
|
||
describe('with invalid GOOGLE_ANALYTICS_ID', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = '<script id="stub" />'; | ||
data = { | ||
config: { | ||
GOOGLE_ANALYTICS_4_ID: '', | ||
}, | ||
}; | ||
loadGoogleAnalytics(data); | ||
body = document.body.innerHTML; | ||
gaScriptSrc = 'https://www.googletagmanager.com/gtag/js?id='; | ||
gaScriptGtag = "gtag('config', '');"; | ||
expect(global.googleAnalytics.invoked).toBeFalsy(); | ||
}); | ||
|
||
it('should not initialize google analytics', () => { | ||
expect(body).not.toMatch(gaScriptSrc); | ||
expect(body).not.toMatch(gaScriptGtag); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
export { default as GoogleAnalyticsLoader } from './GoogleAnalyticsLoader'; |