Skip to content

Data

The data module allows to save and retrieve user data for logged in CrazyGames users. The data will also be synced on all the devices where the user plays the game.

If the user is not logged in, the data module will store the game data in LocalStorage. If the user logs in later, the LocalStorage game data will be synced and backed up on the user's account.

Warning

If you intend to use the data module, don't forget to select the appropriate toggle in the "Does your game save progress?" form when submitting the game. The data module will be disabled otherwise.

Initialization

Before using any methods from the data module, please be sure the SDK is initialized.

await window.CrazyGames.SDK.init();

We recommend that you do this during the loading screen of your game since the SDK preloads all the game data when it is initialized. This may take some time, depending on how much user data is stored.

Using the data module

The data module has the same API as the localStorage:

clear(): void;
getItem(key: string): string | null;
removeItem(key: string): void;
setItem(key: string, value: string): void;

You can call methods from the data module like this:

window.CrazyGames.SDK.data.setItem("gold", 100);

Guest user behaviour

For the guest users, the data module stores the game data in localStorage. When a guest user signs in, you don't need to do anything. Our SDK will automatically load the account game data if there is any, or if this user hasn't played your game before, the SDK will transfer the guest data to the user account.

When the user signs out, the SDK will revert back to using the guest game data.

QA Tool

The data module works in QA Tool, however it doesn't sync the data. This means that regardless of what user you select in the User simulation modal, your game will always have the same data.

Data saving

The SDK debounces data saving with 1 second, meaning that multiple calls to the setItem method will be saved after 1 second. There may be exceptions in various cases, when data saving may be debounced with more time, up to 30 seconds.

There is a 1MB data limit. If you are approaching it, you will see warnings in the browser console. The data won't be backed up anymore if it exceeds 1MB.

Integrating data module into already published games

Since the data module offers the same API as window.localStorage, it is quite easy to integrate it into your already published games. The downside is the data loss that will affect all your players if you don't integrate any migration mechanism from the old data. Since most HTML5 games rely on localStorage for data storage, there are multiple ways to migrate the data from it to the data module.

Populate the data module with old data

If you know all the keys stored in localStorage, you can copy them into the data module at the game start, and remove them from localStorage. This should be done only once for each user when the game starts.

Fallback to localStorage

If the retrieved key from the data module is null, you can fall back to retrieve it from localStorage.