function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hkslhksl 

Lightning Community call apex in <head> section (Napili template)

Is it possible to make apex calls from <head> section of Lightning Community (Napili template)?
We need to get "User Id" inside <head> section to set custom dimension in Google Analytics.
Tried following script that does not work:

<script>
  {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
  {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
    
  .....code provided by Google Analitics here to integrate ........    

  ga('create', '...unique id provided by Google Analitics here...', 'auto');     

  var user = sforce.connection.getUserInfo();  // apex
  console.log(user);

 //need to call:  ga("set", "dimension1", user);  
 
  ga('send', 'pageview');
    
</script>

Any suggestions will be greatly appreciated.
Thank you
Pasan   EeriyagamaPasan Eeriyagama
In a lightning component, would it be possible to call Apex and use below small Apex code segment to get it done.
@AuraEnabled
public static String getUsertId(){
    return UserInfo.getUserId();
}

Further If it's needed to be accessed by another component, an Application event would do I believe.
hkslhksl
Thank you , but I know that. Problem is it's not possible to call google analytoics ga(...) function from code. It seems it can be done only from script in <head> tag. So I need to get value of User Id and pass it to call to ga(...) in <head> tag. I am working on this issue for several days now and latest suggestion was to try to use apex code in <head> tag. 
Any other ideas? I am stuck at this point unfortunately