3

Hello I would like to know what is best way for me to add Google Analytics into SharePoint 2013 On-line. The reason for this because I would like to see break down of usage of space.

1 Answer 1

5

There are 2 ways you can integrate the Google analytics code in SharePoint.

1) Easiest and fastest way is to edit to master page.

a) Open SharePoint designer

b) Navigate to the master page section.

c) Right click and edit your master page in advanced mode.

d) Paste the tracking code just above the closing body tag (</body>) and then check-in and publish your master page.

That's it, you are done. Now wait for 24 Hours and you should see Google Analytics statistics populated based on your site's traffic.

2) Using the User custom action(UCA) route.

According to the Microsoft's latest branding guidelines for SPO, you shouldnt edit the master page. Instead of that you can customize it by injecting the scripts using the UCA.

Here, create a ga.js file in Site assets or style library or any other place where you store artefacts. In this ga.js, paste your google analytics tracking code.

Now Open Visual Studio and create a console application. Add the necessary CSOM dlls. After that, you can use the below code, ensure that the path of js is correct:

UserCustomActionCollection collUserCustomAction = context.Site.UserCustomActions;
UserCustomAction userCustomAction = collUserCustomAction.Add();
userCustomAction.Location = "ScriptLink";                
userCustomAction.Sequence = 9;
userCustomAction.ScriptSrc = "sitecollectionurl/SiteAssets/ga.js";                
userCustomAction.Title = "Google analytics";
userCustomAction.Description = "Google analytics tracking javascript";
userCustomAction.Update();
context.Load(userCustomAction);
context.ExecuteQuery(); 

Here replace sitecollectionurl with your actual site collection url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.