﻿Event.observe(window, "load", _OnInitializeGaEvents);

function _OnInitializeGaEvents()
{
  var config = Proxy.GAnalytics.GetConfiguration();
  if ((config) && (config.Enabled)) {
    Event.observe(document, MAGMA.EVENT.APPLICATION.LOADED, _OnRegisterGaEvents);        
  }
}

function _OnRegisterGaEvents()
{
  Proxy.GetEventSink().observe(UPLATFORM.BOOKUPDATED, _OnGaLogBookUpdated);    
  Proxy.GetEventSink().observe(UPLATFORM.DOCUMENTUPDATED, _OnGaLogDocumentUpdated);  
  Proxy.GetEventSink().observe(UPLATFORM.QUERY.BEFOREEXECUTE, _OnGaLogQuery);
  
  var values = Proxy.GetContext().MagmaLicense.GetValues();                 
  if (values) {    
    Proxy.GAnalytics.Push(["_trackEvent", "UPlatform", "LoggedInUser", unescape(values.License.Id)]);
    Proxy.GAnalytics.Push(["_trackEvent", "UPlatform", "LoggedInOrganisation", unescape(values.License.Organisation)]);
  }  
}

function _OnGaLogBookUpdated(event)
{
  if ((event) && (event.memo) && (Object.isString(event.memo.Id))) {
    Proxy.GAnalytics.Push(["_trackEvent", "UPlatform", "OpenBook", event.memo.Id]);
  }
}

function _OnGaLogDocumentUpdated(event)
{
  if ((event) && (event.memo) && (Object.isString(event.memo.Id))) {
    Proxy.GAnalytics.Push(["_trackEvent", "UPlatform", "OpenDocument", event.memo.Id]);
  }
}

function _OnGaLogQuery(event)
{
  if ((event) && (typeof event.memo == "string")) {    
    Proxy.GAnalytics.Push(["_trackEvent", "UPlatform", "Query", event.memo]);
  }
}