Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Activate Google Analytics in SharePoint Online site
. ".\UserCustomActions.ps1"
Activate Google Analytics
Activate Google Analytics in Office 365/SharePoint Online site
.\Activate-GoogleAnalytics.ps1 -SiteUrl "" -UserName "" -Password "password"
Function Activate-GoogleAnalytics([string]$SiteUrl,[string]$UserName,[string]$Password)
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
$sequenceNo = 1480
#Insert your Tracking code here or specify valid Tracking ID
$TrackingCode = "(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),
ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('send', 'pageview');"
Add-ScriptLinkAction -Context $Context -ScriptBlock $TrackingCode -Sequence $sequenceNo
Activate-GoogleAnalytics -SiteUrl $SiteUrl -UserName $UserName -Password $Password
Copy link

westerdaled commented Jul 18, 2016

Can you please confirm this is scoped so that any site or sub site has this JS injection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment