Using Realm to gather analytics and statistics data in Unity

Hi :slight_smile:

So, I’m not sure if this is the right place for this, but I’m new here, so sorry if I’m wrong or if I use expressions wrongly.

We are currently looking for tools to gather analytics data from our customers for our next project (which will be a Unity game).
We also want to gather data to provide statistics to the player.

I stumbled upon Realm while looking for tools to do this and I thought I can use it for both. But I’m not sure if I got some things right.

So here are a few questions I stumbled upon while I built it into our current project to get a feel for it:

Turn off data collection by switching Realm configuration:
Our players must be able to turn off the collection of analytics data at any time.

My first approach was to switch the Realm configuration to a local Realm after a player turned off data collection.
Then, when a player enables data collection again, I would switch the configuration back to Sync.
When I do that, I get the following error:

RealmException: History type (as specified by the Replication implementation passed to the DB constructor) was not consistent across the session

I guess this happens because the Realm was once synced and can’t be used as a local Realm now?

I would use Flexible Sync for this approach because we don’t want to collect all the data we gather to display the statistics.

Is this a practical approach?
Or does the next approach make more sense?

Turn off data collection by using two different Realms:

My second approach is, to use two different Realms.
One local Realm for gathering statistic data to display to the player and one synced Realm to gather analytics data.
When the player chooses to turn data collection off, I can simply stop to gather data with the synced Realm and log out from the sync session.

I would use Partition Sync for this approach because I can simply sync all the data in the analytics Realm.

The downside of this approach is that some data is duplicated.
For example, in our current project, a duplicate dataset would be how many settlers a player had in the last few in-game years.
But I guess the positives outweigh the negatives with this approach, right?

General data collection

For data collection, I’m thinking about a pull approach, rather than a push approach.

So, rather than pushing data from different parts of the game to the Realm managing part of the game and writing them to the Realm, we want to collect data at fixed time intervals and then write them to the Realm in one go. This would probably happen asynchronously.

As I understand it, the recommended approach is to write as much data as possible in one Write call to the Realm, rather than having many Write calls, right?

Device Sync User

As we don’t want to log in any of our users by e-mail and password, I log in every user as an anonymous user.
Is this the right approach?
Or should we rather not do that?

Hey Stefan, thanks for the well thought out post! Let me try to address some of the points here and pose a few questions.

If I understand correctly, you have two types of data that you want to store in Realm - Analytics (information consumed by you as a developer) and Statistics (information consumed by the player). Do you want the Statistics to be synchronized to the server? This may be useful if you want to allow users to share progress across multiple devices or continue where they left off if they reinstall the game.

And for your questions:

  1. You can convert between local and synchronized Realms using the Realm.WriteCopy method. So theoretically, it’s possible to start out with a synchronized Realm, then convert it a local one, then back to a synchronized one when the user turns back data collection. Note that this doesn’t currently work for flexible sync, but we’re looking at ways to enable it.
  2. You can use a single synchronized Realm for both analytics and statistics. When the player turns data collection off, you can just stop emitting analytics data, while still synchronizing statistics (if that is desirable). If you want to stop sync altogether, you can stop the sync session as soon as the Realm is opened by calling realm.SyncSession.Stop(). This will prevent any synchronization for this Realm until SyncSession.Start is called or the Realm is reopened without stopping the session. If you don’t really need to sync the statistics data, then using a combination of local + synchronized Realm might be best.
  3. Regarding credentials and related to 2. - logging users as anonymous is totally fine. If you want to allow users to share progress across devices, you may consider allowing them to create an account. If they opt to do that, you can link their anonymous credentials with the new credentials they’ve supplied.
  4. For analytics data, we’re in the process of releasing a feature called Asymmetric Sync. This would allow you to more efficiently push unidirectional data (such as analytics) to the server. There’s an open PR for the Unity SDK and we expect to release it in the near future.

Hope this clarifies things a little and happy to continue the conversation. As a big Unity fan, I’m always excited to see what games/projects people are building :smile:

Hi Nikola,

thanks for the quick and detailed answer!

Yes, that’s correct. Currently, we do not plan to synchronise statistics, but we could think about synchronising global progress (i.e. cross-session data).

By “combination” do you mean two separate realms? One for statistics and one for analytics?

I’m looking forward to that! :smiley:

Is there a way to estimate the approximate workload of the Atlas database and therefore the approximate costs, when using Device Sync?

Since we don’t have any analytics in our current game, we don’t have any figures regarding unique users. However, according to Steam, we have about 300 simultaneous players at any given time, with a peak of about 6000 simultaneous players, on the day we left early access.

Of course, we hope that this will increase with the next project :smiley:

Yes, exactly.

@Ian_Ward or @Drew_DiPalma do we have something for this?

Sure you can see our Billing docs here along with examples. -

Thanks, @nirinchev and @Ian_Ward, that helped me a lot!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.