Last active
August 9, 2017 22:02
-
-
Save chrisobriensp/00fab4ea767824b370446dbdd363c34c to your computer and use it in GitHub Desktop.
Adds Google Analytics to every page as part of an SPFx Application Customizer.
This file contains hidden or 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
export default class CobSpFxGaApplicationCustomizer | |
extends BaseApplicationCustomizer<ICobSpFxGaApplicationCustomizerProperties> { | |
@override | |
public onInit(): Promise<void> { | |
/* update with YOUR Google Analytics tracking code: */ | |
let trackingCode: string = "UA-104299656-1"; | |
let scriptTagGA: HTMLScriptElement = document.createElement("script"); | |
scriptTagGA.text = ` | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | |
ga('create', '${trackingCode}', 'auto'); | |
ga('send', 'pageview'); | |
`; | |
scriptTagGA.type = "text/javascript"; | |
document.getElementsByTagName("head")[0].appendChild(scriptTagGA); | |
console.log(`OnInit: Added GA tracking script.`); | |
return Promise.resolve<void>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment