Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local API = {}
- local id = nil
- local remoteEvent = script.ReportGoogleAnalyticsEvent
- local helper = script.GoogleAnalyticsHelper
- local category = "PlaceId: " .. tostring(game.PlaceId) .. " Version: " .. tostring(game.PlaceVersion)
- local googleUserTrackingId = game:GetService("HttpService"):GenerateGUID()
- local lastTimeGeneratedGoogleUserId = os.time()
- local playerJoinTimes = {} -- key : UserId , value : JoinTime
- function convertNewlinesToVertLine(stack)
- local rebuiltStack = ""
- local first = true
- for line in stack:gmatch("[^\r\n]+") do
- if first then
- rebuiltStack = line
- first = false
- else
- rebuiltStack = rebuiltStack .. " | " .. line
- end
- end
- return rebuiltStack
- end
- function removePlayerNameFromStack(stack)
- stack = string.gsub(stack, "Players%.[^.]+%.", "Players.<Player>.")
- return stack
- end
- function setupScriptErrorTracking()
- game:GetService("ScriptContext").Error:connect(function (message, stack)
- API.ReportEvent(category,
- removePlayerNameFromStack(message) .. " | " ..
- removePlayerNameFromStack(stack), "Server Error", 1)
- end)
- -- add tracking for clients
- helper.Parent = game.StarterGui
- -- add to any players that are already in game
- for i, c in ipairs(game.Players:GetChildren()) do
- helper:Clone().Parent = (c:WaitForChild("PlayerGui"))
- end
- end
- function printEventInsteadOfActuallySendingIt(category, action, label, value)
- print("GA EVENT: " ..
- "Category: [" .. tostring(category) .. "] " ..
- "Action: [" .. tostring(action) .. "] " ..
- "Label: [" .. tostring(label) .. "] " ..
- "Value: [" .. tostring(value) .. "]")
- end
- function API.ReportEvent(category, action, label, value)
- if game:FindFirstChild("NetworkServer") ~= nil then
- if id == nil then
- print("WARNING: not reporting event because Init() has not been called")
- return
- end
- -- Try to detect studio start server + player
- if game.CreatorId <= 0 then
- printEventInsteadOfActuallySendingIt(category, action, label, value)
- return
- end
- if os.time() - lastTimeGeneratedGoogleUserId > 7200 then
- googleUserTrackingId = game:GetService("HttpService"):GenerateGUID()
- lastTimeGeneratedGoogleUserId = os.time()
- end
- hs = game:GetService("HttpService")
- hs:PostAsync(
- "http://www.google-analytics.com/collect",
- "v=1&t=event&sc=start" ..
- "&tid=" .. id ..
- "&cid=" .. googleUserTrackingId ..
- "&ec=" .. hs:UrlEncode(category) ..
- "&ea=" .. hs:UrlEncode(action) ..
- "&el=" .. hs:UrlEncode(label) ..
- "&ev=" .. hs:UrlEncode(value),
- Enum.HttpContentType.ApplicationUrlEncoded)
- --printEventInsteadOfActuallySendingIt(category, action, label, value)
- elseif game:FindFirstChild("NetworkClient") ~= nil then
- game:GetService("ReplicatedStorage").ReportGoogleAnalyticsEvent:FireServer(category, action, label, value)
- else
- printEventInsteadOfActuallySendingIt(category, action, label, value)
- end
- end
- function API.Init(userId, config)
- if game:FindFirstChild("NetworkServer") == nil then
- error("Init() can only be called from game server")
- end
- if id == nil then
- if userId == nil then
- error("Cannot Init with nil Analytics ID")
- end
- id = userId
- remoteEvent.Parent = game:GetService("ReplicatedStorage")
- remoteEvent.OnServerEvent:connect(
- function (client, ...) API.ReportEvent(category, ...) end)
- if config == nil or not config["DoNotReportScriptErrors"] then
- setupScriptErrorTracking()
- end
- if config == nil or not config["DoNotTrackServerStart"] then
- API.ReportEvent(category, "Server Startup", "General Statistics", 0)
- end
- if config == nil or not config["DoNotTrackVisits"] then
- game.Players.ChildAdded:connect(function( newPlayer )
- if newPlayer:IsA("Player") then
- API.ReportEvent(category, "Visit", "General Statistics", 1)
- table.insert(playerJoinTimes, newPlayer.userId, os.time() )
- end
- end)
- end
- if config == nil or not config["DoNotTackVisitTimes"] then
- game.Players.ChildRemoved:connect(function( oldPlayer )
- if oldPlayer:IsA("Player") and playerJoinTimes[oldPlayer.userId] then
- local timeValue = ( os.time() - playerJoinTimes[oldPlayer.userId] ) / 60 --Time in Minutes
- timeValue = ( timeValue < 15 and timeValue ) or math.floor(timeValue/5 + .5)*5
- API.ReportEvent(category, tostring(math.floor(timeValue + .5)) .. " Minutes", "Play Time Statistics", 1)
- table.remove(playerJoinTimes, oldPlayer.userId)
- end
- end)
- end
- else
- error("Cannot re-initalize Analytics Module")
- end
- end
- return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement