Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Activate Google Analytics in SharePoint Online site
param(
[string]$SiteUrl,
[string]$UserName,
[string]$Password
)
. ".\UserCustomActions.ps1"
<#
.SYNOPSIS
Activate Google Analytics
.DESCRIPTION
Activate Google Analytics in Office 365/SharePoint Online site
.EXAMPLE
.\Activate-GoogleAnalytics.ps1 -SiteUrl "https://tenant-public.sharepoint.com" -UserName "username@tenant.onmicrosoft.com" -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),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('send', 'pageview');"
Add-ScriptLinkAction -Context $Context -ScriptBlock $TrackingCode -Sequence $sequenceNo
$context.Dispose()
}
Activate-GoogleAnalytics -SiteUrl $SiteUrl -UserName $UserName -Password $Password

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