SharePoint Online Usage Statistics with JavaScript Injection Add-in

%3CLINGO-SUB%20id%3D%22lingo-sub-65536%22%20slang%3D%22en-US%22%3ESharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65536%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20%26nbsp%3Bwant%20to%20add%20Google%20analytics%20to%20every%20site%20on%20our%20Office%20365%20(Classic%20Look%22%20)%20intranet%20%2F%20site%20collection.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20following%20post%20mentions%20using%20Google%20Custom%20Dimensions%20which%20I%20have%20now%20set%20up%20to%20track%20usage%20on%20behalf%20of%20my%20client.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fallcloud.com%2Fblog%2Foptimizing-google-analytics-for-sharepoint-and-office-365%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fallcloud.com%2Fblog%2Foptimizing-google-analytics-for-sharepoint-and-office-365%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERather%20rather%20hack%20the%20master%20page%20%26nbsp%3B(%20see%20I%20was%20paying%20attention%20to%20Vesa%20and%20Waldek)%20%2C%20I%20am%20esssentially%20deploying%20the%26nbsp%3B%3CA%20title%3D%22Core.EmbedJavaScript%20Sample%22%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP%2Ftree%2Fmaster%2FSamples%2FCore.EmbedJavaScript%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ECore.EmbedJavaScript%20Sample%3C%2FA%3E%26nbsp%3Bwith%20a%20different%20payload.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20%3CSTRONG%3Escenaro1.js%3C%2FSTRONG%3E%20I%20have%20added%20this%20globaly%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22kd%22%3Evar%20googleAnalyticsKey%20%3D%20'UA-12345678-1'%3B%20%20%2F%2F%20key%20set%20up%20for%20my%20client%20%3C%2FSPAN%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Efunction%20JavaScript_Embed()%20%7B%0A%0A%20%20%20%20loadScript(jQuery%2C%20function%20()%20%7B%0A%20%20%20%20%20%20%20%20%24(document).ready(function%20()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20message%20%3D%20%22%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2F_Layouts%2FImages%2FSTS_ListItem_43216.gif%22%20align%3D%22absmiddle%22%20%2F%3E%20%3CFONT%20color%3D%22%23AA0000%22%3EDaniel%20is%20attempting%20to%20inject%20GoogleAnalytics%20into%20this%20site...%20it%20could%20get%20%3CI%3Emessy%3C%2FI%3E!%3C%2FFONT%3E%22%3B%0A%0A%20%2F%2F%20Execute%20status%20setter%20only%20after%20SP.JS%20has%20been%20loaded%0A%20SP.SOD.executeOrDelayUntilScriptLoaded(function%20()%20%7B%20SetStatusBar(message)%3B%20%7D%2C%20'sp.js')%3B%0A%0A%0A%20%2F%2F%20Inject%20my%20function%20after%20SP.JS%20has%20been%20loaded%0A%2F%2F%20TODO%20need%20to%20check%2FRefactor%20to%20ensure%20if%20SP.Runtime.js%20is%20also%20loaded%20before%20executing%20the%20function%20below%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSTRONG%3ESP.SOD.executeOrDelayUntilScriptLoaded(SharePointGoogleAnalyticsV2%2C%20'sp.js')%3B%3C%2FSTRONG%3E%3C%2FPRE%3E%3CP%3EHere%20is%20my%20refactored%20(hacked!)%20function%20based%20on%20the%20orginal%20in%20the%20above%20post.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ESharePointGoogleAnalyticsV2%3C%2FSTRONG%3E%3C%2FP%3E%3CPRE%3E%2F%2F%20DW%0A%2F%2F%20rewritten%20to%20hopefully%20use%20SP.SOD.executeOrDelayUntilScriptLoaded()%0Afunction%20SharePointGoogleAnalyticsV2()%20%7B%0A%0A%0A%20%20%20%20%2F%2F%20Google%20Analytics%20funtion%20for%20base%20tracking%0A%20%20%20%20(function%20(i%2C%20s%2C%20o%2C%20g%2C%20r%2C%20a%2C%20m)%20%7B%0A%20%20%20%20%20%20%20%20i%5B'GoogleAnalyticsObject'%5D%20%3D%20r%3B%20i%5Br%5D%20%3D%20i%5Br%5D%20%7C%7C%20function%20()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20(i%5Br%5D.q%20%3D%20i%5Br%5D.q%20%7C%7C%20%5B%5D).push(arguments)%0A%20%20%20%20%20%20%20%20%7D%2C%20i%5Br%5D.l%20%3D%201%20*%20new%20Date()%3B%20a%20%3D%20s.createElement(o)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20m%20%3D%20s.getElementsByTagName(o)%5B0%5D%3B%20a.async%20%3D%201%3B%20a.src%3Dg%3B%20m.parentNode.insertBefore(a%2C%20m)%0A%20%20%20%20%7D)(window%2C%20document%2C%20'script'%2C%20'https%3A%2F%2Fwww.google-analytics.com%2Fanalytics.js'%2C%20'ga')%3B%0A%0A%0A%20%20%20%20%2F%2F%20custom%20code%20to%20seed%20Diminension%20with%20metadata%20from%20visits%20to%20each%3A%20Site%2FPage%2FList%2FItem%20per%20User%0A%20%20%20%20var%20clientContext%20%3D%20new%20SP.ClientContext()%3B%0A%20%20%20%20var%20site%20%3D%20clientContext.get_site()%3B%0A%20%20%20%20var%20rootWeb%20%3D%20site.get_rootWeb()%3B%0A%20%20%20%20var%20web%20%3D%20clientContext.get_web()%3B%0A%20%20%20%20var%20user%20%3D%20web.get_currentUser()%3B%0A%20%20%20%20clientContext.load(site)%3B%0A%20%20%20%20clientContext.load(rootWeb)%3B%0A%20%20%20%20clientContext.load(web)%3B%0A%20%20%20%20clientContext.load(user)%3B%0A%0A%20%20%20%20var%20listId%20%3D%20_spPageContextInfo.pageListId%3B%0A%20%20%20%20var%20itemId%20%3D%20_spPageContextInfo.pageItemId%3B%0A%20%20%20%20var%20list%20%3D%20null%3B%0A%20%20%20%20var%20item%20%3D%20null%3B%0A%20%20%20%20if%20(listId%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20list%20%3D%20web.get_lists().getById(listId)%3B%0A%20%20%20%20%20%20%20%20clientContext.load(list%2C%20'Title'%2C%20'DefaultViewUrl')%3B%0A%0A%20%20%20%20%20%20%20%20if%20(itemId%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20item%20%3D%20list.getItemById(itemId)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20clientContext.load(item%2C%20'Id'%2C%20'DisplayName')%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20function%20SPGASend()%20%7B%0A%20%20%20%20%20%20%20%20ga('create'%2C%20googleAnalyticsKey%2C%20'auto')%3B%0A%0A%20%20%20%20%20%20%20%20var%20userNameParts%20%3D%20user.get_loginName().split('%7C')%3B%0A%0A%20%20%20%20%20%20%20%20ga('send'%2C%20'pageview'%2C%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension1'%3A%20rootWeb.get_title()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension2'%3A%20site.get_url()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension3'%3A%20web.get_title()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension4'%3A%20web.get_url()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension5'%3A%20listId%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension6'%3A%20(list%20%3F%20list.get_title()%20%3A%20'')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension7'%3A%20(list%20%3F%20list.get_defaultViewUrl()%20%3A%20'')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension8'%3A%20itemId%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension9'%3A%20(item%20%3F%20item.get_displayName()%20%3A%20'')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension10'%3A%20(item%20%3F%20(list.get_defaultViewUrl()%20%2B%20'%3FID%3D'%20%2B%20item.get_id())%20%3A%20'')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension11'%3A%20user.get_title()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'dimension12'%3A%20userNameParts%5BuserNameParts.length%20-%201%5D%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20function%20SPGAError(errObj%2C%20errMsg)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Optional%20error%20handling.%0A%20%20%20%20%7D%0A%0A%20%20%20%20clientContext.executeQueryAsync(Function.createDelegate(this%2C%20SPGASend)%2C%20Function.createDelegate(this%2C%20SPGAError))%3B%0A%20%20%20%20%20%20%20%20%0A%20%20%0A%7D%3C%2FPRE%3E%3CP%3EThis%20now%20compiles%20and%20deploys%20to%20my%20Developer%20site%20.%20The%20Google%20Analytics%20Page%20for%20my%20client's%20tenant%20shows%201%20active%20user%20(see%20below)%20which%20hopefully%20is%20me.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3EDoes%20my%20refactor%20look%20ok%20and%20will%20insert%20the%20script%20block(s)%20as%20per%20the%20post%20-%20Chrome%20Developer%20tools%20shows%20scenario1.js%20is%20being%20loaded%3F%20I%20am%20just%20not%20100%25%20sure%20the%20function%20is%20working%20correctly.%20Normally%20the%20analytics%20can%20be%20captured%20after%20a%20few%20days%20so%20I%20need%20to%20make%20sure%20all%20is%20well%20before%20deploying%20to%20the%20Intranet.%3C%2FLI%3E%3CLI%3EIf%201)%20looks%20good%20can%20I%20deploy%20the%20the%20Add-in%20to%20a%20standard%20Site%20collection%2C%20so%20not%20just%20the%20Developer%20Collection%3C%2FLI%3E%3CLI%3EWill%20I%20need%20to%20deploy%20the%20Add-In%20to%20each%20team%20site%20in%20my%20standard%20site%20collection%20as%20essentially%20each%20master%20page%20on%20each%20Team%20Site%20will%20need%20to%20be%20running%20the%20script%20block.%20%3CEM%3E%26nbsp%3BIf%20can%20I%20use%20a%20provisioning%20template%20to%20add%20or%20remove%20my%20Add-in%20on%20each%20Team%20Site%3F%3C%2FEM%3E%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F14040iEC4D9EBB46D15F18%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22GoogleAnalytics.PNG%22%20title%3D%22GoogleAnalytics.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-65536%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-280606%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-280606%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20links%20I%20provided%20are%20generic%20for%20injecting%20any%20javascript%20or%20css%20into%20web%20pages.%20It%20doesn't%20provide%20any%20injection%20or%20tracking%20of%20documents%20(online%2Foffline).%20You%20will%20need%20to%20check%20with%20the%20analytics%20vendor%20on%20how%20to%20tracking%20documents%20-%20probably%20modifying%20the%20links%20on%20the%20pages%20to%20include%20a%20tracking%20code%2C%20but%20that%20is%20beyond%20the%20scope%20of%20this%20thread.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-280592%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-280592%22%20slang%3D%22en-US%22%3E%3CP%3EDoes%20this%20usage%20analytics%20inject%20javascript%20to%20only%20web%20pages%20%3F%20Would%20this%20work%20for%20usage%20analytics%20of%20documents%20being%20looked%20in%20office%20online%2Fbrowser%20%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-69206%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-69206%22%20slang%3D%22en-US%22%3E%3CP%3EI%20haven't%20seen%20that%20happen%20unless%20you%20are%20doing%20something%20in%20your%20script%20that%20overrides%20or%20manipulates%20the%20SP%20DOM.%3C%2FP%3E%3CP%3EMaybe%20you%20are%20right%20about%20the%20sequence%20number%20and%20try%20that.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-69204%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-69204%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F51898%22%20target%3D%22_blank%22%3E%40Steve%20Borgwardt%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20applied%20the%20script%2C%20yesterday%20evening%20on%20our%20branded%20test%20site%20.%20%26nbsp%3BThe%20good%20news%20I%20am%20able%20to%20see%20some%20of%20my%20Custom%20Dimensions%20on%20our%20%3CA%20href%3D%22https%3A%2F%2Fanalytics.google.com%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fanalytics.google.com%3C%2FA%3E%26nbsp%3Bportal.%20The%20bad%20news%20is%20we%20experienced%20some%20unfortunate%20side%20effects%20...%20%26nbsp%3BNo%20ribbon%20%26nbsp%3Bbar%20on%20list%20settings%2C%20some%20web%20part%20pages%20not%20showing%20content.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBefore%20I%20think%20of%20a%20rewriting%20say%20using%20JSCore%20%26amp%3B%20batching%20I%20think%20I%20will%20change%20sequence%20and%20also%20deal%20wth%20the%20clash%20shown%20below%20-%20this%20is%20an%20Add-in%20installed%20years%20ago%20that%20nobody%20seems%20to%20remember%20anything%20about%20%3B-(.%20%26nbsp%3B%20%26nbsp%3BAs%20anybody%20experienced%20similar%20issues%20with%20Custom%20Actions%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F14453i1AA6B75D30907766%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22ExtraCAre%20Intranet%20User%20Custom%20Actions%20ECSharePointGoogleAnalytics%20Installed%20v2.png%22%20title%3D%22ExtraCAre%20Intranet%20User%20Custom%20Actions%20ECSharePointGoogleAnalytics%20Installed%20v2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-68465%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-68465%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20Daniel%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGlad%20to%20hear%20you%20are%20getting%20closer%20with%20your%20solution.%20In%20regards%20to%20that%20first%20client%20script%20action%20(PnP)%2C%20I%20believe%20you%20still%20need%20that.%20But%20you%20may%20want%20to%20clean%20everything%20up%20first%20(remove%20all%20script%20actions)%20and%20then%20uses%20the%20PnP%20injection%20script%20method.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-68413%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-68413%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F51898%22%20target%3D%22_blank%22%3E%40Steve%20Borgwardt%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJust%20a%20quickup%20update%20%2C%20%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20now%20tried%20a%20few%20methods%20for%20injecting%20my%20analytics%20script%20into%20my%20site%20or%20web.%20%26nbsp%3B%20Looks%20like%20a%20script%20link%20is%20better%20than%20a%20script%20block%20.%20%26nbsp%3BAlso%20I%20am%20getting%20mixed%20results%20with%20a%20heavily%20branded%20site.%20Thanks%20for%20your%20previous%20actions%20as%20I%20did%20use%20it%20to%20inect%20a%20simpel%20script%20block%20into%20my%20iste%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGot%20really%20stuck%20on%20John%20Liu's%20%26nbsp%3B%3CA%20href%3D%22http%3A%2F%2Fjohnliu.net%2Fblog%2F2015%2F12%2Fconvert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototype%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fjohnliu.net%2Fblog%2F2015%2F12%2Fconvert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototype%3C%2FA%3E%20%26nbsp%3B...%20%26nbsp%3BI%20seem%20never%20to%20get%20passed%20the%20error%20'Protoype%20not%20defined'%20.%20Spoke%20to%20Hugh%20from%20Rencore%20about%20his%20custom%20SOD%20loader%20for%20sp.js%20and%20runtime.js%20but%20his%20answer%20gave%20me%20a%20headache%20trying%20to%20understand%20it%20lol....%20Level%20400%20JavaScript%20springs%20to%20mind!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnnyway%20John%20kindly%20pointed%20at%20this%20one%20page%20add%20your%20script%20.....%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fjohnliu.net%2Fblog%2F2015%2F12%2Fthe-safest-future-proof-way-to-brand-your-sharepoint-and-sharepoint-online%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fjohnliu.net%2Fblog%2F2015%2F12%2Fthe-safest-future-proof-way-to-brand-your-sharepoint-and-sharepoint-online%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOk%20before%20I%20am%20ready%20to%20inject%20my%20final%20script%20I%20do%20need%20to%20remove%20the%20first%20script%20action%20with%20the%20client%20id%20shown%20(%20see%20attachment)%20.%20It%20is%20part%20of%20the%20PnP%20CoreJavaScriptInjection%20which%20despite%20sucessfully%20deploying%20via%20VS2017%20and%20removing%20via%20the%20UI%20%2C%20I%20can't%20find%20a%20way%20of%20remove%20this%20Action%20link.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-67497%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-67497%22%20slang%3D%22en-US%22%3E%3CP%3ESteve%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BI%20can%20finally%20report%20some%20success.%20To%20make%20this%20easier%20I%20created%20two%20functions%2C%20one%20based%20on%20John's%20example%20and%20one%20I%20put%20together%20by%20looking%20at%20the%20link%20you%20gave%20and%20seeing%20if%20there%20is%20another%20way%20to%20write%20it.%20%26nbsp%3Bmy%20second%20function%20works%20and%26nbsp%3Bgives%20no%20errors%20in%20the%20console.%20%26nbsp%3B%20The%20PS%20works%20like%20a%20charm%20and%20simply%20replaces%20my%20customSciptAction%20when%20I%20call%20my%20set%20up%20function.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Efunction%20debugit2()%20%7B%0A%0A%20%20%20%20console.log(%22Debugit2%20Started....%22)%3B%0A%20%20%20%20var%20ctx%20%3D%20SP.ClientContext.get_current()%3B%0A%20%20%20%20var%20web%20%3D%20ctx.get_web()%3B%0A%20%20%20%20functionToCall().done(function%20(returnedValue)%20%7B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20do%20something%20with%20the%20returned%20value%0A%20%20%20%20%20%20%20%20console.log(web.get_title())%3B%0A%20%20%20%20%7D)%3B%0A%0A%20%20%20%20function%20functionToCall()%20%7B%0A%0A%20%20%20%20%20%20%20%20var%20dfd%20%3D%20%24.Deferred(function%20()%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx.load(web)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20ctx.executeQueryAsync(function%20()%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dfd.resolve(web)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20function%20(sender%2C%20args)%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fthrow%20an%20error%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20failmessage%20%3D%20args%5B1%5D.get_message()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20console.log(%22%20oh%20no%20not%20again%20%22%20%2B%20failmessage)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%20%20%20%20return%20dfd.promise(%20%2F*%20do%20I%20chain%20here%20to%20say%20get%20my%20user%20or%20site%20or%20list%20objects%20*%2F%20%20%20%20%20)%3B%0A%0A%20%20%20%20%7D%0A%0A%7D%3C%2FPRE%3E%3CP%3EThe%20only%20thing%20I%20need%20to%20do%20now%20is%20chain%20my%20promises%20to%20get%20the%20jsom%20objects%3A%20user%2C%20list%2C%20site%20%26nbsp%3Betc%2C%20rename%20my%20function%20calls%20and%20then%20re-add%20my%20google%20delegate%20code.%20%26nbsp%3BI%20think%20I%20hit%20the%20pain%20barrier%20on%20this%20%3B-(%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-67339%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-67339%22%20slang%3D%22en-US%22%3E%3CP%3ESteve%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3EThanks%20for%20your%20reply%2C%20I%20had%20based%20my%20rewrite%20on%20John%20Liu's%20example%20which%20baffled%20me%20why%20I%20was%20getting%20my%20error%3C%2FLI%3E%3C%2FOL%3E%3CP%3EYou%20raise%20an%20interesting%20point%20my%20calling%20loadscript%20function%20is%20currently%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EloadScript(%22https%3A%2F%2Fajax.aspnetcdn.com%2Fajax%2FjQuery%2Fjquery-2.0.2.min.js%22%2C%20function%20()%20%7B%0A%20%20%20%20console.log(%22jQuery%20loaded%20but%20who%20cares%20as%20we%20are%20applying%20Google%20Analytics....%20Yeah!%22)%3B%0A%0A%20%20%20%20%2F%2F%20Inject%20my%20function%20after%20SP.JS%20has%20been%20loaded%0A%20%20%20%20%2F%2F%20TODO%20need%20to%20check%2FRefactor%20to%20ensure%20if%20SP.Runtime.js%20is%20also%20loaded%20before%20executing%20the%20function%20below%0A%20%20%20%20SP.SOD.executeOrDelayUntilScriptLoaded(SharePointGoogleAnalyticsV3%2C%20'sp.js')%3B%0A%0A%0A%0A%7D)%3B%3C%2FPRE%3E%3CP%3EI%20am%20wondering%20if%20I%20sould%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Echange%20the%20sp.SOD%20to%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%2F%2F%20embedding%20of%20jQuery%2C%20and%20initialization%20of%20responsiveness%20when%20ready%20%0A%2F%2F%20changed%20to%20use%20a%20CDN%0AloadScript(%22https%3A%2F%2Fajax.aspnetcdn.com%2Fajax%2FjQuery%2Fjquery-2.0.2.min.js%22%2C%20function%20()%20%7B%0A%20%20%20%20console.log(%22jQuery%20loaded%20but%20who%20cares%20as%20we%20are%20applying%20Google%20Analytics....%20Yeah!%22)%3B%0A%0A%20%20%20%20%2F%2F%20Inject%20my%20function%20after%20SP.JS%20has%20been%20loaded%0A%20%20%20%20%2F%2F%20TODO%20need%20to%20check%2FRefactor%20to%20ensure%20if%20SP.Runtime.js%20is%20also%20loaded%20before%20executing%20the%20function%20below%0A%20%20%0ASP.SOD.executeFunc('sp.js'%2C%20'SP.ClientContext'%2C%20SharePointGoogleAnalyticsV3)%3B%0A%0A%7D)%3B%0A%0A%3C%2FPRE%3E%3CP%3E..%20%26nbsp%3Bone%20thing%20I%20have%20now%20noticed%20is%20the%20log%20is%20showing%20a%20message%20which%20I%20have%20commented%20out%20in%20my%20function%20with%20promises.%20%26nbsp%3BIn%20the%20morrning%20I%20will%20manually%20remove%20the%20script%20I%20deploy%20or%20use%20powershell%20to%20remove%20old%20if%20exists%20...%20%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-67199%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-67199%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20I%20would%20first%20start%20by%20making%20sure%20sp.js%20is%20loaded%20-%20add%20this%20line%20above%20that%20code%3C%2FP%3E%3CPRE%3ESP.SOD.executeFunc('sp.js'%2C%20'SP.ClientContext'%2C%20sharePointReady)%3B%3C%2FPRE%3E%3CP%3EWhere%20'sharePointReady'%20is%20the%20function%20name%20around%20your%20code%20%3A%3C%2FP%3E%3CPRE%3Efunction%20sharePointReady()%20%7B%0A%20%20%20%20clientContext%20%3D%20SP.ClientContext.get_current()%3B%0A%20%20%20%20website%20%3D%20clientContext.get_web()%3B%0A%0A%20%20%20%20clientContext.load(website)%3B%0A%20%20%20%20clientContext.executeQueryAsync(onRequestSucceeded%2C%20onRequestFailed)%3B%0A%7D%3C%2FPRE%3E%3CP%3EAlso%2C%20you%20are%20calling%20clientContext.%3CSTRONG%3EexecuteQuery%3C%2FSTRONG%3E%20(which%20is%20not%20supported%20using%20JSOM%2C%20you%20should%20use%20executeQueryAsync()%3C%2FP%3E%3CPRE%3EclientContext.load(website)%3B%0A%20%20%20%20clientContext.executeQueryAsync(onRequestSucceeded%2C%20onRequestFailed)%3B%0A%7D%0Afunction%20onRequestSucceeded()%20%7B%0A%20%20%20%20alert(website.get_url())%3B%0A%7D%0Afunction%20onRequestFailed(sender%2C%20args)%20%7B%0A%20%20%20%20alert('Error%3A%20'%20%2B%20args.get_message())%3B%0A%7D%3C%2FPRE%3E%3CP%3EIf%20you%20want%20to%20use%20promises%20-%20I%20have%20seen%20some%20articles%20where%20they%20create%20a%20prototype%20of%20executeQueryAsync%3C%2FP%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fjohnliu.net%2Fblog%2F2015%2F12%2Fconvert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototype%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fjohnliu.net%2Fblog%2F2015%2F12%2Fconvert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototype%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnother%20example%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fblog.qumsieh.ca%2F2013%2F10%2F31%2Fusing-jquery-promises-deferreds-with-sharepoint-2013-jsom%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fblog.qumsieh.ca%2F2013%2F10%2F31%2Fusing-jquery-promises-deferreds-with-sharepoint-2013-jsom%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-67079%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-67079%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20thought%20I%20would%20rewrite%20the%20jsom%20calls%20to%20use%20promises%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%20%20%2F%2F%20custom%20code%20to%20seed%20Diminension%20with%20metadata%20from%20visits%20to%20each%3A%20Site%2FPage%2FList%2FItem%20per%20User%0A%20%20%20%20var%20clientContext%20%3D%20SP.ClientContext.get_current()%3B%0A%20%20%20%20%2F%2F%0A%20%20%20%20%2F%2F%20get%20the%20site%20collection%20details%0A%20%20%20%20%2F%2F%0A%0A%0A%20%20%20%20var%20site%20%3D%20clientContext.get_site()%3B%0A%20%20%20%20var%20rootWeb%20%3D%20site.get_rootWeb()%3B%0A%20%20%20%20var%20web%20%3D%20clientContext.get_web()%3B%0A%20%20%20%20var%20user%20%3D%20web.get_currentUser()%3B%0A%0A%0A%20%20%20%20clientContext.load(site)%3B%0A%20%20%20%20var%20promise%20%3D%20clientContext.executeQuery()%3B%20%2F%2F%20look!%20%20%3A-)%0A%20%20%20%20promise.done(function%20()%20%7B%0A%20%20%20%20%20%20%20%20console.log(site.get_title())%3B%0A%20%20%20%20%7D)%3B%0A%0A%20%20%20%20promise.then(function%20(sArgs)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2FsArgs%5B0%5D%20%3D%3D%20success%20callback%20sender%0A%20%20%20%20%20%20%20%20%2F%2FsArgs%5B1%5D%20%3D%3D%20success%20callback%20args%0A%20%20%20%20%20%20%20%20console.log(site.get_url())%3B%0A%0A%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20%2F%2F%20get%20web%20details%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20clientContext.load(web)%3B%0A%20%20%20%20%20%20%20%20var%20promise%20%3D%20clientContext.executeQuery()%3B%20%2F%2F%20look!%20%20%3A-)%0A%20%20%20%20%20%20%20%20promise.done(function%20()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(web.get_title())%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%20%20%20%20promise.then(function%20(sArgs)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FsArgs%5B0%5D%20%3D%3D%20success%20callback%20sender%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FsArgs%5B1%5D%20%3D%3D%20success%20callback%20args%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20get%20User%20details%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20clientContext.load(user)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20promise%20%3D%20clientContext.executeQuery()%3B%20%2F%2F%20look!%20%20%3A-)%0A%20%20%20%20%20%20%20%20%20%20%20%20promise.done(function%20()%20%7B%0A%0A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20console.log(user.get_title())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20promise.then(function%20(sArgs)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FsArgs%5B0%5D%20%3D%3D%20success%20callback%20sender%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FsArgs%5B1%5D%20%3D%3D%20success%20callback%20args%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%20function%20(fArgs)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FfArgs%5B0%5D%20%3D%3D%20fail%20callback%20sender%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FfArgs%5B1%5D%20%3D%3D%20fail%20callback%20args.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fin%20JSOM%20the%20callback%20args%20aren't%20used%20much%20-%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fthe%20only%20useful%20one%20is%20probably%20the%20get_message()%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fon%20the%20fail%20callback%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20failmessage%20%3D%20fArgs%5B1%5D.get_message()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20console.log(failmessage)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%0A%0A%0A%0A%20%20%20%20%20%20%20%20%7D%2C%20function%20(fArgs)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FfArgs%5B0%5D%20%3D%3D%20fail%20callback%20sender%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FfArgs%5B1%5D%20%3D%3D%20fail%20callback%20args.%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fin%20JSOM%20the%20callback%20args%20aren't%20used%20much%20-%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fthe%20only%20useful%20one%20is%20probably%20the%20get_message()%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fon%20the%20fail%20callback%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20failmessage%20%3D%20fArgs%5B1%5D.get_message()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(failmessage)%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%7D%2C%20function%20(fArgs)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2FfArgs%5B0%5D%20%3D%3D%20fail%20callback%20sender%0A%20%20%20%20%20%20%20%20%2F%2FfArgs%5B1%5D%20%3D%3D%20fail%20callback%20args.%0A%20%20%20%20%20%20%20%20%2F%2Fin%20JSOM%20the%20callback%20args%20aren't%20used%20much%20-%20%0A%20%20%20%20%20%20%20%20%2F%2Fthe%20only%20useful%20one%20is%20probably%20the%20get_message()%20%0A%20%20%20%20%20%20%20%20%2F%2Fon%20the%20fail%20callback%0A%20%20%20%20%20%20%20%20var%20failmessage%20%3D%20fArgs%5B1%5D.get_message()%3B%0A%20%20%20%20%20%20%20%20console.log(failmessage)%3B%0A%20%20%20%20%7D)%3B%0A%0A%3C%2FPRE%3E%3CP%3EBut%20sigh%20..%20I%20get%20yet%20another%20runtime%20error%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20737px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F14134i664A791E4D72E254%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22GoogleAnalytics%20error%20again.PNG%22%20title%3D%22GoogleAnalytics%20error%20again.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66870%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66870%22%20slang%3D%22en-US%22%3E%3CP%3ESteve%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20kept%20the%20JQuery%20load%20in%20for%20the%20time%20being%20and%20as%20you%20you%20say%20I%20have%20a%20custom%20JS%20which%20is%20loaded%20by%20the%20script%20Action.%20%26nbsp%3BI%20am%20getting%20much%20further%20thanks%20to%20your%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20now%20see%20some%20of%20my%20random%20log%20statements%20(!)%20and%20it%20looks%20like%20some%20of%20the%20csom%20code%20that%3C%2FP%3E%3CP%3Eupdates%20the%20google%20dimensions%20is%20working%20such%20as%20%22User%22%20and%20%22List%22.%20I%20will%20need%20to%20debug%20this%20to%20see%20why%20the%20%22Web%22%20isn't%20being%20loaded%20.%20%26nbsp%3B%20%22Worse%20case%22%20I%20could%20use%20SPJSCore%20library%20but%20I%20think%20this%20overkill%20given%20my%20modest%20requirements%20.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20845px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F14093iD03162B3D89DDE82%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22issues%20with%20CustomscriptAction.PNG%22%20title%3D%22issues%20with%20CustomscriptAction.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66806%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66806%22%20slang%3D%22en-US%22%3E%3CP%3Eyou%20don't%20necessarily%20need%20that%20load%20script%20for%20jQuery%20-%20that%20was%20just%20a%20sample%20they%20put%20in.%3C%2FP%3E%3CP%3EI%20assume%20your%20function%26nbsp%3B%3C%2FP%3E%3CPRE%3E(SharePointGoogleAnalyticsV2)%3C%2FPRE%3E%3CP%3Eis%20in%20the%20custom.js%20file%20that%20is%20being%20deployed%20by%20the%20PS%20script%3F%3C%2FP%3E%3CP%3EThat%20should%20work%20then%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66773%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66773%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20replying.%26nbsp%3B%20I%20have%20now%20cloned%20and%20modfied%20the%20sample%20Custom-Script-Action.js%20as%20mentionded%20in%20your%20reply%20%26nbsp%3Bto%20include%20the%20main%20parts%20of%20my%20Scenario1.js%20...%20I%20am%20not%20100%25%20this%20correct%20as%20in%20do%20I%20need%20to%20wait%20for%20sp.js%20(%20and%20%26nbsp%3Bruntime.js)%20to%20be%20loaded.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%2F%2F%20embedding%20of%20jQuery%2C%20and%20initialization%20of%20responsiveness%20when%20ready%20%0A%2F%2F%20changed%20to%20use%20a%20CDN%0AloadScript(%22https%3A%2F%2Fajax.aspnetcdn.com%2Fajax%2FjQuery%2Fjquery-2.0.2.min.js%22%2C%20function%20()%20%7B%0A%20%20%20%20console.log(%22jQuery%20loaded%20and%20ready%20for%20custom%20JS%22)%3B%0A%0A%20%20%20%20%2F%2F%20Inject%20my%20function%20after%20SP.JS%20has%20been%20loaded%0A%20%20%20%20%2F%2F%20TODO%20need%20to%20check%2FRefactor%20to%20ensure%20if%20SP.Runtime.js%20is%20also%20loaded%20before%20executing%20the%20function%20below%0A%20%20%20%20SP.SOD.executeOrDelayUntilScriptLoaded(SharePointGoogleAnalyticsV2%2C%20'sp.js')%3B%0A%0A%0A%0A%7D)%3B%3C%2FPRE%3E%3CP%3EI%20will%20test%20out%20shortly%20as%20I%20am%20meant%20to%20also%20be%20writing%20a%20training%20course%20%3B-(%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66539%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20Usage%20Statistics%20with%20JavaScript%20Injection%20Add-in%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66539%22%20slang%3D%22en-US%22%3E%3CP%3EInstead%20of%20using%20that%20Add-in%20model%2C%20I%20would%20look%20at%20using%20this%20open%20source%20solution%2C%20which%20is%20based%20on%20the%20PnP%20responsive%20UI%20project%20and%20works%20on%20the%20new%20modern%20doc%20libraries%20also.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20title%3D%22SharePoint%20Online%20Custom%20Action%20Script%20Reference%22%20href%3D%22https%3A%2F%2Fgithub.com%2Feoverfield%2FSP-Custom-Script-Action%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Feoverfield%2FSP-Custom-Script-Action%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20will%20inject%20your%20Javascript%20using%20the%20User%20Custom%20Action%20method.%20Just%20modify%20the%202%20XML%20PnP%20Provisioning%20template%20files%20to%20reference%20your%20.js%20file.%20The%20PS%20script%20will%20upload%20the%20.js%20file%20to%20the%20location%20you%20specify%20in%20template%20file%20and%20then%20inject%20it%20into%20every%20page%20in%20the%20site%20collection.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20you%20will%20have%20to%20run%20this%20PS%20against%20each%20site%20collection%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Regular Contributor

Hi

 

I  want to add Google analytics to every site on our Office 365 (Classic Look" ) intranet / site collection.

 

The following post mentions using Google Custom Dimensions which I have now set up to track usage on behalf of my client. 

https://allcloud.com/blog/optimizing-google-analytics-for-sharepoint-and-office-365/

 

Rather rather hack the master page  ( see I was paying attention to Vesa and Waldek) , I am esssentially deploying the Core.EmbedJavaScript Sample with a different payload. 

 

In scenaro1.js I have added this globaly 

   

var googleAnalyticsKey = 'UA-12345678-1';  // key set up for my client 

 

function JavaScript_Embed() {

    loadScript(jQuery, function () {
        $(document).ready(function () {
            var message = "<img src='/_Layouts/Images/STS_ListItem_43216.gif' align='absmiddle'> <font color='#AA0000'>Daniel is attempting to inject GoogleAnalytics into this site... it could get <i>messy</i>!</font>";

 // Execute status setter only after SP.JS has been loaded
 SP.SOD.executeOrDelayUntilScriptLoaded(function () { SetStatusBar(message); }, 'sp.js');


 // Inject my function after SP.JS has been loaded
// TODO need to check/Refactor to ensure if SP.Runtime.js is also loaded before executing the function below
            SP.SOD.executeOrDelayUntilScriptLoaded(SharePointGoogleAnalyticsV2, 'sp.js');

Here is my refactored (hacked!) function based on the orginal in the above post. 

SharePointGoogleAnalyticsV2

// DW
// rewritten to hopefully use SP.SOD.executeOrDelayUntilScriptLoaded()
function SharePointGoogleAnalyticsV2() {


    // Google Analytics funtion for base tracking
    (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');


    // custom code to seed Diminension with metadata from visits to each: Site/Page/List/Item per User
    var clientContext = new SP.ClientContext();
    var site = clientContext.get_site();
    var rootWeb = site.get_rootWeb();
    var web = clientContext.get_web();
    var user = web.get_currentUser();
    clientContext.load(site);
    clientContext.load(rootWeb);
    clientContext.load(web);
    clientContext.load(user);

    var listId = _spPageContextInfo.pageListId;
    var itemId = _spPageContextInfo.pageItemId;
    var list = null;
    var item = null;
    if (listId != null) {
        list = web.get_lists().getById(listId);
        clientContext.load(list, 'Title', 'DefaultViewUrl');

        if (itemId != null) {
            item = list.getItemById(itemId);
            clientContext.load(item, 'Id', 'DisplayName');
        }
    }

    function SPGASend() {
        ga('create', googleAnalyticsKey, 'auto');

        var userNameParts = user.get_loginName().split('|');

        ga('send', 'pageview', {
            'dimension1': rootWeb.get_title(),
            'dimension2': site.get_url(),
            'dimension3': web.get_title(),
            'dimension4': web.get_url(),
            'dimension5': listId,
            'dimension6': (list ? list.get_title() : ''),
            'dimension7': (list ? list.get_defaultViewUrl() : ''),
            'dimension8': itemId,
            'dimension9': (item ? item.get_displayName() : ''),
            'dimension10': (item ? (list.get_defaultViewUrl() + '?ID=' + item.get_id()) : ''),
            'dimension11': user.get_title(),
            'dimension12': userNameParts[userNameParts.length - 1]
        });
    }

    function SPGAError(errObj, errMsg) {
        // Optional error handling.
    }

    clientContext.executeQueryAsync(Function.createDelegate(this, SPGASend), Function.createDelegate(this, SPGAError));
        
  
}

This now compiles and deploys to my Developer site . The Google Analytics Page for my client's tenant shows 1 active user (see below) which hopefully is me.

 

  1. Does my refactor look ok and will insert the script block(s) as per the post - Chrome Developer tools shows scenario1.js is being loaded? I am just not 100% sure the function is working correctly. Normally the analytics can be captured after a few days so I need to make sure all is well before deploying to the Intranet.
  2. If 1) looks good can I deploy the the Add-in to a standard Site collection, so not just the Developer Collection
  3. Will I need to deploy the Add-In to each team site in my standard site collection as essentially each master page on each Team Site will need to be running the script block.  If can I use a provisioning template to add or remove my Add-in on each Team Site?

 

 GoogleAnalytics.PNG

 

 

14 Replies
Highlighted

Instead of using that Add-in model, I would look at using this open source solution, which is based on the PnP responsive UI project and works on the new modern doc libraries also.

 

https://github.com/eoverfield/SP-Custom-Script-Action

 

It will inject your Javascript using the User Custom Action method. Just modify the 2 XML PnP Provisioning template files to reference your .js file. The PS script will upload the .js file to the location you specify in template file and then inject it into every page in the site collection.

 

So you will have to run this PS against each site collection

 

Thanks

Highlighted

Hi

 

Thanks for replying.  I have now cloned and modfied the sample Custom-Script-Action.js as mentionded in your reply  to include the main parts of my Scenario1.js ... I am not 100% this correct as in do I need to wait for sp.js ( and  runtime.js) to be loaded.  

 

 

// embedding of jQuery, and initialization of responsiveness when ready 
// changed to use a CDN
loadScript("https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2.min.js", function () {
    console.log("jQuery loaded and ready for custom JS");

    // Inject my function after SP.JS has been loaded
    // TODO need to check/Refactor to ensure if SP.Runtime.js is also loaded before executing the function below
    SP.SOD.executeOrDelayUntilScriptLoaded(SharePointGoogleAnalyticsV2, 'sp.js');



});

I will test out shortly as I am meant to also be writing a training course ;-(

 

 

Highlighted

you don't necessarily need that load script for jQuery - that was just a sample they put in.

I assume your function 

(SharePointGoogleAnalyticsV2)

is in the custom.js file that is being deployed by the PS script?

That should work then 

Highlighted

Steve

 

 

I have kept the JQuery load in for the time being and as you you say I have a custom JS which is loaded by the script Action.  I am getting much further thanks to your help.

 

I can now see some of my random log statements (!) and it looks like some of the csom code that

updates the google dimensions is working such as "User" and "List". I will need to debug this to see why the "Web" isn't being loaded .   "Worse case" I could use SPJSCore library but I think this overkill given my modest requirements .

 

 

issues with CustomscriptAction.PNG

Highlighted

Hi 

 

 

I thought I would rewrite the jsom calls to use promises 

 

  // custom code to seed Diminension with metadata from visits to each: Site/Page/List/Item per User
    var clientContext = SP.ClientContext.get_current();
    //
    // get the site collection details
    //


    var site = clientContext.get_site();
    var rootWeb = site.get_rootWeb();
    var web = clientContext.get_web();
    var user = web.get_currentUser();


    clientContext.load(site);
    var promise = clientContext.executeQuery(); // look!  :-)
    promise.done(function () {
        console.log(site.get_title());
    });

    promise.then(function (sArgs) {
        //sArgs[0] == success callback sender
        //sArgs[1] == success callback args
        console.log(site.get_url());


        //
        // get web details
        //
        clientContext.load(web);
        var promise = clientContext.executeQuery(); // look!  :-)
        promise.done(function () {
            console.log(web.get_title());
        });

        promise.then(function (sArgs) {
            //sArgs[0] == success callback sender
            //sArgs[1] == success callback args

            //
            // get User details
            //

            clientContext.load(user);
            var promise = clientContext.executeQuery(); // look!  :-)
            promise.done(function () {



                console.log(user.get_title());
            })
            promise.then(function (sArgs) {
                //sArgs[0] == success callback sender
                //sArgs[1] == success callback args
            }, function (fArgs) {
                //fArgs[0] == fail callback sender
                //fArgs[1] == fail callback args.
                //in JSOM the callback args aren't used much - 
                //the only useful one is probably the get_message() 
                //on the fail callback
                var failmessage = fArgs[1].get_message();
                console.log(failmessage);
            });




        }, function (fArgs) {
            //fArgs[0] == fail callback sender
            //fArgs[1] == fail callback args.
            //in JSOM the callback args aren't used much - 
            //the only useful one is probably the get_message() 
            //on the fail callback
            var failmessage = fArgs[1].get_message();
            console.log(failmessage);
        });

    }, function (fArgs) {
        //fArgs[0] == fail callback sender
        //fArgs[1] == fail callback args.
        //in JSOM the callback args aren't used much - 
        //the only useful one is probably the get_message() 
        //on the fail callback
        var failmessage = fArgs[1].get_message();
        console.log(failmessage);
    });

But sigh .. I get yet another runtime error

 

GoogleAnalytics error again.PNG

Highlighted

Hi, I would first start by making sure sp.js is loaded - add this line above that code

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

Where 'sharePointReady' is the function name around your code :

function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    website = clientContext.get_web();

    clientContext.load(website);
    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}

Also, you are calling clientContext.executeQuery (which is not supported using JSOM, you should use executeQueryAsync()

clientContext.load(website);
    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
function onRequestSucceeded() {
    alert(website.get_url());
}
function onRequestFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

If you want to use promises - I have seen some articles where they create a prototype of executeQueryAsync

http://johnliu.net/blog/2015/12/convert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototy...

 

Another example:

http://blog.qumsieh.ca/2013/10/31/using-jquery-promises-deferreds-with-sharepoint-2013-jsom/

 

Thanks

 

 

Highlighted

Steve,

 

 

  1. Thanks for your reply, I had based my rewrite on John Liu's example which baffled me why I was getting my error

You raise an interesting point my calling loadscript function is currently 

 

loadScript("https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2.min.js", function () {
    console.log("jQuery loaded but who cares as we are applying Google Analytics.... Yeah!");

    // Inject my function after SP.JS has been loaded
    // TODO need to check/Refactor to ensure if SP.Runtime.js is also loaded before executing the function below
    SP.SOD.executeOrDelayUntilScriptLoaded(SharePointGoogleAnalyticsV3, 'sp.js');



});

I am wondering if I sould 

 

change the sp.SOD to 

 

// embedding of jQuery, and initialization of responsiveness when ready 
// changed to use a CDN
loadScript("https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2.min.js", function () {
    console.log("jQuery loaded but who cares as we are applying Google Analytics.... Yeah!");

    // Inject my function after SP.JS has been loaded
    // TODO need to check/Refactor to ensure if SP.Runtime.js is also loaded before executing the function below
  
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', SharePointGoogleAnalyticsV3);

});

..  one thing I have now noticed is the log is showing a message which I have commented out in my function with promises.  In the morrning I will manually remove the script I deploy or use powershell to remove old if exists ...  

Highlighted

Steve

 

 I can finally report some success. To make this easier I created two functions, one based on John's example and one I put together by looking at the link you gave and seeing if there is another way to write it.  my second function works and gives no errors in the console.   The PS works like a charm and simply replaces my customSciptAction when I call my set up function.

 

function debugit2() {

    console.log("Debugit2 Started....");
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    functionToCall().done(function (returnedValue) {

        // do something with the returned value
        console.log(web.get_title());
    });

    function functionToCall() {

        var dfd = $.Deferred(function () {

            

            ctx.load(web);

            ctx.executeQueryAsync(function () {

                dfd.resolve(web);

            },

                function (sender, args) {

                    //throw an error
                    var failmessage = args[1].get_message();
                    console.log(" oh no not again " + failmessage);
                });

        });

        return dfd.promise( /* do I chain here to say get my user or site or list objects */     );

    }

}

The only thing I need to do now is chain my promises to get the jsom objects: user, list, site  etc, rename my function calls and then re-add my google delegate code.  I think I hit the pain barrier on this ;-( 

 

Highlighted

@Steve Borgwardt

 

 

Just a quickup update ,   

 

I have now tried a few methods for injecting my analytics script into my site or web.   Looks like a script link is better than a script block .  Also I am getting mixed results with a heavily branded site. Thanks for your previous actions as I did use it to inect a simpel script block into my iste

 

Got really stuck on John Liu's  http://johnliu.net/blog/2015/12/convert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototy...  ...  I seem never to get passed the error 'Protoype not defined' . Spoke to Hugh from Rencore about his custom SOD loader for sp.js and runtime.js but his answer gave me a headache trying to understand it lol.... Level 400 JavaScript springs to mind!

 

Annyway John kindly pointed at this one page add your script .....

 

http://johnliu.net/blog/2015/12/the-safest-future-proof-way-to-brand-your-sharepoint-and-sharepoint-...

 

 

Ok before I am ready to inject my final script I do need to remove the first script action with the client id shown ( see attachment) . It is part of the PnP CoreJavaScriptInjection which despite sucessfully deploying via VS2017 and removing via the UI , I can't find a way of remove this Action link.

Highlighted

Hi, Daniel

 

Glad to hear you are getting closer with your solution. In regards to that first client script action (PnP), I believe you still need that. But you may want to clean everything up first (remove all script actions) and then uses the PnP injection script method.

 

Thanks

Highlighted

@Steve Borgwardt

 

 

I applied the script, yesterday evening on our branded test site .  The good news I am able to see some of my Custom Dimensions on our https://analytics.google.com portal. The bad news is we experienced some unfortunate side effects ...  No ribbon  bar on list settings, some web part pages not showing content.  

 

Before I think of a rewriting say using JSCore & batching I think I will change sequence and also deal wth the clash shown below - this is an Add-in installed years ago that nobody seems to remember anything about ;-(.    As anybody experienced similar issues with Custom Actions? 

 

 

 

 

ExtraCAre Intranet User Custom Actions ECSharePointGoogleAnalytics Installed v2.png

 

Highlighted

I haven't seen that happen unless you are doing something in your script that overrides or manipulates the SP DOM.

Maybe you are right about the sequence number and try that.

 

Thanks

Highlighted

Does this usage analytics inject javascript to only web pages ? Would this work for usage analytics of documents being looked in office online/browser ?

Highlighted

The links I provided are generic for injecting any javascript or css into web pages. It doesn't provide any injection or tracking of documents (online/offline). You will need to check with the analytics vendor on how to tracking documents - probably modifying the links on the pages to include a tracking code, but that is beyond the scope of this thread.