Note: Remember you can click/touch over the images in order to enlarge them.
Implementing your own statistics is incredibly useful since you can customize and track all the data you want, you can track any action in your game and create reports based on the data you have captured, reports that will be very useful to analyze game status, players behavior and to make decisions based on your findings.
By combining SQL Reporting Services with Web API 2, you can create a cross platform system.
The only other required component is the actual client which will send the data, in Unity we can take advantage of the WWW class to build the message and post the data to the server.
First, in the server project, we implement a class to handle our data, in this case, we created "GameSessionStatsModel
", since we are tracking per session data.
![](https://2.bp.blogspot.com/-TZIiOWIJJCA/VXmtGJivveI/AAAAAAAADh0/D1bVil8QQFo/s400/GameSessionStatsModelClass.png)
Then we implement the actual Controller, we named it "GameStatsController
".
![](https://2.bp.blogspot.com/-dSWFneOtgJI/VXmt0fLOK8I/AAAAAAAADh8/stgtQPglIps/s400/GameStatsController.png)
We now proceed to implement the Post action.
![](https://1.bp.blogspot.com/-BGLaaQpVzJM/VXmud6UGavI/AAAAAAAADiE/jUMqXzOxNV4/s400/GameStatsControllerPostAction.png)
In this action, we mark the parameter with [FromBody]
because we have the data in the body of the message we receive, you could have it with [FromUri]
instead in case you are sending the data in the URL.
Now, if you are using Unity, you can take advantage of coroutines, and the WWW class.
![](https://3.bp.blogspot.com/-5bv6IaSCabc/VXmyNuwv7aI/AAAAAAAADiQ/qo_HP1od3vY/s400/GameStats_UpdateServerStats.png)
In this method, we create an instance of the WWWForm
, we specify that the Content-Type
is going to be in json format with the frm.headers["Content-Type"]
.
To add the field we need to send, we use the .AddFields
methods of the WWWForm
instance object,
the name of the string
must match the exact name of the properties in the class GameSessionStatsModel
implemented before.
Then we build the URL we need to send, by default Web API 2 will be [serverUrl]/api/[controller]
Since our controller class is named "GameStatsController
" our URL will be [serverUrl]/api/GameStats
.
Note: Replace serverurl with your correct URL.
Then, we create an instance of the WWW class, we send the URL and the form as parameters in the constructor, and we use the yield
keyword to execute the request and wait for a response to be received, since in our code we are constantly updating data, we also use WaitForSeconds
, with the desired amount of seconds, in this case we are using 5 seconds for testing purposes.
Now, let's go to the reports.
Use the Business Intelligence templates to create a Reporting project in Visual Studio, and create a new empty report.
![](https://1.bp.blogspot.com/-GpxcBM8uboc/VXm1RcMGGkI/AAAAAAAADic/1pbNERjepv0/s400/GameStatsPlatformUsageReport_Designer.png) |
Design Mode |
![](https://4.bp.blogspot.com/-crmjTUscYyM/VXm3AWwQv7I/AAAAAAAADis/QBMjJ8UBZzA/s400/GameStatsPlatformUsageReport_DesignerPreview.png) |
Preview Mode |
In this case, we created a report to see how many sessions exist for each single platform we send data from.
Hopefully it has been useful to you.