Last active
August 29, 2015 14:15
-
-
Save pkskelly/3b7d8af4b9d03f6f6834 to your computer and use it in GitHub Desktop.
Adds a Google Analytics script to all /teams/* site collections in an Office 365 Tenant.
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
<# | |
.SYNOPSIS | |
Adds a Google Analytics script to all /teams/* site collections in an Office | |
365 Tenant. | |
.DESCRIPTION | |
When using Office 365 "/teams/*" managed path site collections as an extranet, some | |
clients would like to track their traffic using Google Analytics. This is a | |
simple script that adds the Google Analytics script to a UserCustomAction | |
using the http://github.com/officedev.pnp PowerShell cmdlet's to manage SharePoint | |
Online. | |
NOTE: There is a better way to do this! Using the Site extension method AddJsBlock | |
would only require this to be done once per site collection. There is an open | |
Issue in PnP (https://github.com/OfficeDev/PnP/issues/454) which would enable PowerShell | |
use of Site objects when calling AddJsBlock. I will update the script when this | |
functionality is incorported into the cmdlets. | |
This script requires the Office Dev PnP PowerShell Cmdlets. To install, visit | |
https://github.com/OfficeDev/PnP/tree/master/Binaries/PowerShell.Commands and select | |
the proper MSI package for you environment. | |
DISCLAIMER: Script provided AS IS with no warranty. Your mileage will vary. Use | |
this script on a production tenant AT YOUR OWN RISK. | |
.EXAMPLE | |
.\Add-GoogleAnalytics.ps1 -tenant https://twofficepnp1-admin.sharepoint.com -gaid UA-888889-00 | |
#> | |
Param( | |
[parameter(Mandatory=$true)][alias("tenant")]$TenantUrl="", | |
[Parameter(Mandatory=$false)][alias("gaid")]$GoogleId="" | |
) | |
#Managed path to query for all site collections | |
$managedPath = "*teams*" | |
$ga_script = @" | |
(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', '$GoogleId', 'auto'); | |
ga('send', 'pageview'); | |
"@; | |
function AddGoogleScript | |
{ | |
param | |
( | |
$web | |
) | |
#Remove any previously deployed script | |
Remove-SPOJavaScriptLink -Key "GoogleAnalytics" -Force -Web $web | |
Write-Host " Removed script from " $web.Url | |
#Add the analytics script | |
Add-SPOJavaScriptBlock -Key "GoogleAnalytics" -Script $ga_script -web $web | |
Write-Host " Added script to " $web.Url | |
} | |
function WalkWeb | |
{ | |
param | |
( | |
$web, | |
[System.Management.Automation.PSCredential] $creds | |
) | |
Write-Host " Changing script for web " $web.Url | |
AddGoogleScript( $web ) | |
#connect to the web to get a new client context | |
Connect-SPOnline -Url $web.Url -Credentials $creds | |
$subwebs = Get-SPOSubWebs | |
if ($null -ne $subwebs) | |
{ | |
foreach($child in $subwebs) | |
{ | |
Write-Host " Walking web " $child.Url | |
WalkWeb $child $creds | |
} | |
} | |
else | |
{ | |
Write-Host " No sub-webs for " $web.Url | |
} | |
} | |
######################################################## | |
## ## | |
## Main script ## | |
## ## | |
######################################################## | |
#Get the tenant level site to query all site collections | |
$creds = Get-Credential -Message "Enter Tenant Administrator Credentials" | |
#Connect to the Admin Tenant to query all site collections in managed path | |
Connect-SPOnline -Url $TenantUrl -Credentials $creds | |
#Get all site collections filtered to the teams managed path | |
$siteCollections = Get-SPOTenantSite | Where {$_.Url -like $managedPath} | Select Url | |
foreach($site in $siteCollections) | |
{ | |
Write-Host "Connecting to site collection " $site.Url | |
#connect to the site collection to get a new client context | |
Connect-SPOnline -Url $site.Url -Credentials $creds | |
$rootWeb = Get-SPOWeb | |
WalkWeb $rootWeb $creds | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment