Advertisement
nickmaster24

MainModule

Aug 12th, 2014
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. local API = {}
  2.  
  3. local id = nil
  4. local remoteEvent = script.ReportGoogleAnalyticsEvent
  5. local helper = script.GoogleAnalyticsHelper
  6. local category = "PlaceId: " .. tostring(game.PlaceId) .. " Version: " .. tostring(game.PlaceVersion)
  7. local googleUserTrackingId = game:GetService("HttpService"):GenerateGUID()
  8. local lastTimeGeneratedGoogleUserId = os.time()
  9.  
  10. local playerJoinTimes = {} -- key : UserId , value : JoinTime
  11.  
  12. function convertNewlinesToVertLine(stack)
  13.     local rebuiltStack = ""
  14.     local first = true
  15.     for line in stack:gmatch("[^\r\n]+") do
  16.         if first then
  17.             rebuiltStack = line
  18.             first = false
  19.         else
  20.             rebuiltStack = rebuiltStack .. " | " .. line
  21.         end
  22.     end
  23.     return rebuiltStack
  24. end
  25.  
  26. function removePlayerNameFromStack(stack)
  27.     stack = string.gsub(stack, "Players%.[^.]+%.", "Players.<Player>.")
  28.     return stack
  29. end
  30.  
  31. function setupScriptErrorTracking()
  32.     game:GetService("ScriptContext").Error:connect(function (message, stack)
  33.         API.ReportEvent(category,
  34.             removePlayerNameFromStack(message) .. " | " ..
  35.             removePlayerNameFromStack(stack), "Server Error", 1)
  36.     end)
  37.     -- add tracking for clients
  38.     helper.Parent = game.StarterGui
  39.     -- add to any players that are already in game
  40.     for i, c in ipairs(game.Players:GetChildren()) do
  41.         helper:Clone().Parent = (c:WaitForChild("PlayerGui"))
  42.     end
  43. end
  44.  
  45. function printEventInsteadOfActuallySendingIt(category, action, label, value)
  46.     print("GA EVENT: " ..
  47.         "Category: [" .. tostring(category) .. "] " ..
  48.         "Action: [" .. tostring(action) .. "] " ..
  49.         "Label: [" .. tostring(label) .. "] " ..
  50.         "Value: [" .. tostring(value) .. "]")
  51. end
  52.  
  53. function API.ReportEvent(category, action, label, value)
  54.     if game:FindFirstChild("NetworkServer") ~= nil then
  55.         if id == nil then
  56.             print("WARNING: not reporting event because Init() has not been called")
  57.             return
  58.         end
  59.        
  60.         -- Try to detect studio start server + player
  61.         if game.CreatorId <= 0 then
  62.             printEventInsteadOfActuallySendingIt(category, action, label, value)
  63.             return
  64.         end
  65.        
  66.         if os.time() - lastTimeGeneratedGoogleUserId > 7200 then
  67.             googleUserTrackingId = game:GetService("HttpService"):GenerateGUID()
  68.             lastTimeGeneratedGoogleUserId = os.time()
  69.         end
  70.  
  71.         hs = game:GetService("HttpService")
  72.         hs:PostAsync(
  73.             "http://www.google-analytics.com/collect",
  74.             "v=1&t=event&sc=start" ..
  75.             "&tid=" .. id ..
  76.             "&cid=" .. googleUserTrackingId ..
  77.             "&ec=" .. hs:UrlEncode(category) ..
  78.             "&ea=" .. hs:UrlEncode(action) ..
  79.             "&el=" .. hs:UrlEncode(label) ..
  80.             "&ev=" .. hs:UrlEncode(value),
  81.             Enum.HttpContentType.ApplicationUrlEncoded)
  82.        
  83.         --printEventInsteadOfActuallySendingIt(category, action, label, value)
  84.     elseif game:FindFirstChild("NetworkClient") ~= nil then
  85.         game:GetService("ReplicatedStorage").ReportGoogleAnalyticsEvent:FireServer(category, action, label, value)
  86.     else
  87.         printEventInsteadOfActuallySendingIt(category, action, label, value)
  88.     end
  89. end
  90.  
  91. function API.Init(userId, config)
  92.     if game:FindFirstChild("NetworkServer") == nil then
  93.         error("Init() can only be called from game server")
  94.     end
  95.     if id == nil then
  96.         if userId == nil then
  97.             error("Cannot Init with nil Analytics ID")
  98.         end
  99.  
  100.         id = userId
  101.         remoteEvent.Parent = game:GetService("ReplicatedStorage")
  102.         remoteEvent.OnServerEvent:connect(
  103.             function (client, ...) API.ReportEvent(category, ...) end)
  104.        
  105.         if config == nil or not config["DoNotReportScriptErrors"] then
  106.             setupScriptErrorTracking()
  107.         end
  108.  
  109.         if config == nil or not config["DoNotTrackServerStart"] then
  110.             API.ReportEvent(category, "Server Startup", "General Statistics", 0)
  111.         end
  112.        
  113.         if config == nil or not config["DoNotTrackVisits"] then
  114.             game.Players.ChildAdded:connect(function( newPlayer )
  115.                 if newPlayer:IsA("Player") then
  116.                     API.ReportEvent(category, "Visit", "General Statistics", 1)
  117.                     table.insert(playerJoinTimes, newPlayer.userId, os.time() )
  118.                 end
  119.             end)
  120.         end
  121.        
  122.         if config == nil or not config["DoNotTackVisitTimes"] then
  123.             game.Players.ChildRemoved:connect(function( oldPlayer )
  124.                 if oldPlayer:IsA("Player") and playerJoinTimes[oldPlayer.userId] then
  125.                     local timeValue = ( os.time() - playerJoinTimes[oldPlayer.userId] ) / 60 --Time in Minutes
  126.                     timeValue = ( timeValue < 15 and timeValue ) or math.floor(timeValue/5 + .5)*5
  127.                     API.ReportEvent(category, tostring(math.floor(timeValue + .5)) .. " Minutes", "Play Time Statistics", 1)
  128.                     table.remove(playerJoinTimes, oldPlayer.userId)
  129.                 end
  130.             end)
  131.         end
  132.     else
  133.         error("Cannot re-initalize Analytics Module")
  134.     end
  135. end
  136.  
  137. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement