Question

I have created a Page in SharePoint online and it contains an embed code from another service that we utilize. I want this service to track the user that viewed the page within the embed. To do this, I need to add "?id=usersemailaddress" to the URL AFTER the user has launched the page. I am using the code below, but it fails at window.history.pushstate. Any help would be appreciated.

$(function() {
  document.title = "Custom Title";
  $( "#ctl00_fullscreenmodeBtn" ).hide();
});
SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function(){  
  var context = new SP.ClientContext.get_current();
  var web = context.get_web();
  var currentUser = web.get_currentUser();
  context.load(currentUser);
  context.executeQueryAsync(
  function(){
    console.log(currentUser.get_email()); 
      if (window.location.href  === "https://example.sharepoint.com/sites/test/SitePages/test.aspx") {
         window.history.pushState("object or string", "Title", "https://example.sharepoint.com/sites/test/SitePages/test.aspx?id=" + currentUser.get_email());
      }
    }, 
  function(sender, args){
    console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
  });
});
Was it helpful?

Solution

you should try this :

var url = window.location.href;    
if (url.indexOf('?') > -1){
   url += '&param=1'
}else{
   url += '?param=1'
}
window.location.href = url;
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top