Forum Discussion
SharePoint Online Usage Statistics with JavaScript Injection Add-in
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
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 ;-(
- Steve BorgwardtMay 03, 2017Brass Contributor
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
- Daniel WesterdaleMay 03, 2017Iron Contributor
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 .
- Daniel WesterdaleMay 04, 2017Iron Contributor
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