Skip to content

Getting started with MRGSFirebase🔗

MRGSFirebase integration requires a few simple steps:

Step 1. Import the MRGSFirebase module🔗

  • Add MRGSFirebase module

    Unity:

    Adding to the project (general instruction)

    Step 1. Add Sources

    To add MRGS to a project via the Unity Package Manager (available from Unity 2018+) simply add a scopedRegistries section to the Packages/manifest.json file by adding the following entry:

    {
        "dependencies": {
            ...
        },
        "scopedRegistries": [
                {
                    "name": "MRGS",
                    "url": "https://mrgs-nexus.my.games/repository/mrgs-uninty-plugins/",
                    "scopes": [
                        "games.my.mrgs"
                    ]
                }
        ]
    }
    

    Alternatively, you can click Edit -> Project Settings -> Package Manager -> '+' in scoped registry section, and fill in the fields according to the data above.

    Step 2. Add dependency

    • Click Window -> Package Manager -> select 'Packages: MyRegistries' from dropdown list, select package MRGSFirebase from the list, then click "Install"
    • Import the module: using MRGS;
    • Download the latest version of the library. Unzip the archive.
    • (For unitypackage integration) In Unity, click Assets -> Import Package -> Custom Package, and select the games.my.mrgs.firebase.unitypackage package from the downloaded archive.
    • (For tgz integration) In Unity, click Window -> Package Manager -> '+' -> Add package from tarball, and select the games.my.mrgs.firebase-<version> package. tgz from the downloaded archive.
    • Import the module: using MRGS;

    iOS:

    Adding to the project (general instruction)

    Step 1: Add dependencies

    Via Package collection

    • In Xcode select File > Add Packages
    • Select "+" > "Add Swift Package Collection"
    • Insert URL: https://mrgs-nexus.my.games/repository/ios-sdks/MRGSPackageCollection.json
    • Select module MRGSFirebase from "MRGS Package Collection".
    • Or you can select "MRGS" package from "MRGS Package Collection" (contains all mrgs modules as products) and then select "MRGS/Firebase" product only.

    Individual packages

    • In Xcode select File > Add Packages
    • In the search bar at the top right, paste the URL: https://mrgs-gitea.my.games/mrgs/mrgsfirebase-ios-sdk.git
    • Add a module to your project
    • Or you can paste the url https://mrgs-gitea.my.games/mrgs/ios-sdks.git to include the "MRGS" package which contains all mrgs modules as products and then select only the product" MRGS/Firebase".

    Step 2: Add support for ObjectiveC categories

    • Set the -ObjC flag in the "Other linker Flags" field in the project settings.
    • Import the module in code: @import MRGServiceKit; or @import MRGSFirebase; or #import <MRGSFirebase/MRGSFirebase.h>

    Step 1. Add Sources

    In your podfile, add sources to the top of the file:

    source 'https://github.com/CocoaPods/Specs.git' # For main repo
    source 'https://mrgs-gitea.my.games/mrgs/cocoapods-specs.git'  # For MRGS repo
    

    Step 2: Add dependencies

    Add the latest version of MRGSFirebase to target:

    To add via subspecs:

    target 'MyProject' do
        pod 'MRGS', '~> 5.0.0', :subspecs => ['Firebase']
    end
    

    To add via individual modules:

    target 'MyProject' do
        pod 'MRGSFirebase', '~> 5.0.0'
    end
    

    To add all mrgs modules:

    target 'MyProject' do
        pod 'MRGS/AllKits', '~> 5.0.0'
    end
    

    Step 3: Install dependencies

    • Run pod install (or pod install --repo-update if necessary)
    • Import the module in code: @import MRGServiceKit; or @import MRGSFirebase; or #import <MRGSFirebase/MRGSFirebase.h>

    Step 1: Add dependencies

    Add the dependency to your Cartfile:

    binary "https://mrgs-nexus.my.games/repository/ios-sdks/MRGSFirebase/MRGSFirebase.json" ~> 5.0.0
    

    Step 2: Install dependencies

    • Run carthage update --use-xcframeworks
    • Add downloaded frameworks to your project (make sure "do not embed" option is enabled)
    • Set the -ObjC flag in the "Other linker Flags" field in the project settings.
    • Import the module in code: @import MRGServiceKit; or @import MRGSFirebase; or #import <MRGSFirebase/MRGSFirebase.h>
    • Download the latest version of the library. Unzip the archive.
    • Add MRGSFirebase.xcframework from the downloaded archive to your project (Drag the libraries to the "Linked frameworks and Libraries" section) (Also contains MRGSFirebase.framework for compatibility - fat framework)

    • Set the -ObjC flag in the "Other linker Flags" field in the project settings.

    • Import the module in code: @import MRGSFirebase; or #import <MRGSFirebase/MRGSFirebase.h>
    • Also, you can add the MRGServiceKit.h and module.modulemap files from the archive to your project, or specify the path to them in the project settings in the Build Settings -> Header search paths section. Now, instead of importing each of our frameworks separately, you can only import one header file: @import MRGServiceKit;

    Android:

    Add a dependency in the root build.gradle file:

    dependencies {
        // Root gradle
        //...
        classpath 'com.android.tools.build:gradle:7.4.2'
    
        // Add it.
        classpath 'com.google.gms:google-services:4.3.13'
    
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    

    Add a dependency in the project build.gradle file:

    dependencies {
        def mrgsVersion = "6.x.x"
    
        implementation "games.my.mrgs:firebase:$mrgsVersion"
    }
    

    Copy the MRGSFirebase.aar file to the "libs" directory of your project. Add the necessary dependencies to the "build.gradle" file.

    dependencies {
        // Root gradle
        //...
        classpath 'com.android.tools.build:gradle:7.4.2'
    
        // Add it.
        classpath 'com.google.gms:google-services:4.3.13'
    
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    
    dependencies {
        // App module
        //...
        implementation(name: 'MRGSFirebase', ext:'aar')
    
        implementation 'androidx.appcompat:appcompat:1.6.1'
        implementation 'com.google.firebase:firebase-analytics:21.3.0'
    }
    

Huawei Store

Please note that Huawei no longer supplies Google Service on its devices, which makes Firebase unavailable.

Step 2. Set up the application on the Firebase website🔗

  • On the Firebase website, create an application using the Google Firebase documentation.
  • Open the application settings section.
Application settings section

firebase_project_settings

  • Download files google-services.json for android and GoogleService-Info.plist for iOS.
Configuration files download

firebase_project_settings

firebase_project_settings

Step 3. Add downloaded files to the project🔗

Unity🔗

For Android:

Copy the file google-services.json in Assets/Plugins/Android/assets. During the build assembly, Unity will place the google-services.json file in the src/main/assets directory of the Android project. You need to configure the build so that this file is in the root of the assembled Android project. The better way to do this is by editing a gradle file (See the Unity< your_version > tab) in the Assets/Plugins/Android directory and placing the following code at the very end of the file:

Unity 2018

Edit the baseProjectTemplate.gradle file.

dependencies {
    classpath 'com.android.tools.build:gradle:7.4.2'

    // Add it.
    classpath 'com.google.gms:google-services:4.3.13'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

Edit the mainTemplate.gradle file.

if ('**APPLICATIONID**' == <YOUR_APP_PACKAGE_NAME>)
{
    copy {
        from "./src/main/assets/"
        into "./"
        include 'google-services.json'
    }

    apply plugin: 'com.google.gms.google-services'
}

Unity 2019

Edit the baseProjectTemplate.gradle file.

dependencies {
    classpath 'com.android.tools.build:gradle:7.0.3'

    // Add it.
    classpath 'com.google.gms:google-services:4.3.10'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

Edit the launcherTemplate.gradle file.

if ('**APPLICATIONID**' == <YOUR_APP_PACKAGE_NAME>)
{
    copy {
        from "../unityLibrary/src/main/assets/"
        into "./"
        include 'google-services.json'
    }

    apply plugin: 'com.google.gms.google-services'
}

Attention

<YOUR_APP_PACKAGE_NAME> is the package name of your Android project, and the string apply plugin: 'com.google.gms.google-services' is needed to parse the file google-services.json.

For iOS:

Copy the file GoogleService-Info.plist in Assets/Plugins/iOS. MRGS will automatically add this file to the Xcode project in the "Copy Bundle Resources" section during assembly. In case you do not want to put the file in Assets/Plugins/iOS, you can put it anywhere, but in this case you will need to add a copy of this file (using PostBuildProcess) to the Xcode project, the code below is snippet for files copying:

Example of copying a file to a project using PostBuildProcess
public class MRGSPostBuildExample
{
    private const string ConfigsPath = "Assets/Path/Configs";

    [PostProcessBuild]
    public static void PostProcessBuild(BuildTarget target, string path)
    {
        if (target == BuildTarget.iOS)
        {
#if UNITY_IOS
            string projPath = PBXProject.GetPBXProjectPath(path);
            PBXProject proj = new PBXProject();
            proj.ReadFromString(File.ReadAllText(projPath));

#if UNITY_2019_3_OR_NEWER
            string projectTarget = proj.GetUnityMainTargetGuid();
#else
            string targetName = PBXProject.GetUnityTargetName();
            string projectTarget = proj.TargetGuidByName(targetName);
#endif

            AddLocalFile(path, proj, projectTarget, ConfigsPath, "GoogleService-Info.plist");

            File.WriteAllText(projPath, proj.WriteToString());
#endif
        }
    }
#if UNITY_IOS
    private static void AddLocalFile(string buildPath, PBXProject proj, string targetName, string fileDir, string fileName)
    {
        var filePath = Path.Combine(fileDir, fileName);
        File.Copy(filePath, Path.Combine(buildPath, fileName), true);
        proj.AddFileToBuild(targetName, proj.AddFile(fileName, fileName, PBXSourceTree.Source));
    }
#endif
}

iOS🔗

Add the file GoogleService-Info.plist (downloaded earlier from the Firebase admin panel) to the project (Copy Bundle Resources section in the Build Phases tab)

Android🔗

Copy the google-services.json file to the root folder of your project. Usually, it's the app directory. Edit the file build.gradle and add the following line at the end of the file:

apply plugin: 'com.google.gms.google-services'

Step 4. Sending events🔗

User properties🔗

User properties are attributes you define to describe segments of your user base, such as language preference or geographic location.

To set user properties for sending to firebase analytics, use the method:

...
MRGSFirebase.Instance.SetUserProperty("property_name", "property_value");
@import MRGSFirebase;

[MRGSFirebase setUserPropertyString:@"value" forName:@"property"];
import games.my.mrgs.firebase.MRGSFirebaseAnalytics;

MRGSFirebaseAnalytics.getInstance().setUserProperty("property_name", "property_value");

Custom events🔗

Use the following API to send your own events:

var params = new Dictionary<string,object>();
params["someInfoKey"] = "someImportantInfo";
MRGSFirebase.Instance.TrackEvent("event_name", params);
@import MRGSFirebase;

[[MRGSFirebase sharedInstance] trackEvent:@"user_did_pass_level" eventParams:@{@"level":@"1"}];
Map<String, Object> params = new TreeMap<>();
params.put("someInfoKey", "someImportantInfo")
MRGSFirebaseAnalytics.getInstance().sendEvent("event_name", params);

Auto-collected events🔗

MRGS automatically monitors and sends additional launch and payment events to Firebase.

  1. Regardless of the use of other modules, MRGS will automatically send events:

    Event Sending condition
    mrgs_cumulative_session_time_30min Sent when the user has been in the application for more than 30 minutes. Dispatched once.
    mrgs_cumulative_session_time_60min Sent when the user has been in the application for more than 60 minutes. Dispatched once.
    mrgs_cumulative_session_time_120min Sent when the user has been in the application for more than 120 minutes. Dispatched once.
    mrgs_cumulative_session_time_180min Sent when the user has been in the application for more than 180 minutes. Dispatched once.
  2. MRGS will also send an additional list of payment events, but only if one of the conditions is met:

    These conditions are not mandatory, they can be omitted if you do not need payment event data.

    Event Sending condition
    Purchases1D Dispatched upon purchase within one day of user registration
    Purchases7D Dispatched upon purchase within one week of user registration
    Purchases14D Dispatched upon purchase within two weeks of user registration
    Purchases28D Sent upon purchase within one month of user registration
    Purchases90D Dispatched upon purchase within three months of user registration
    UniquePurchases1D Sent for a purchase within one day of the user's registration, if that purchase was the first purchase they made
    UniquePurchases7D Sent for a purchase within one week of a user's registration if that purchase was the first purchase they made
    UniquePurchases14D Sent for a purchase within two weeks of a user's registration if that purchase was the first purchase they made
    UniquePurchases28D Sent for a purchase within one month of the user's registration, if that purchase was the first purchase they made
    UniquePurchases90D Sent for a purchase within three months of a user registered if the purchase was the first purchase they made
    mrgs_valid_inapp_purchase Sent after the purchase has been validated, and the payment information is also added to the event (sku, price, currency)
    mrgs_valid_inapp_5_dollars_plus Dispatched when any purchase exceeds $ 5.
    mrgs_valid_inapp_10_dollars_plus Dispatched when any purchase exceeds $ 10.
    mrgs_valid_inapp_50_dollars_plus Dispatched when any purchase exceeds $ 50.
    mrgs_valid_inapp_100_dollars_plus Dispatched for any purchase over $ 100.

    Test purchases

    Since MRGS SDK 6.4.0 version, test purchases will be sent to Firebase but value of such purchases will be 0.

Getting application instance id🔗

To get the application id use the method:

MRGSFirebase.Instance.GetAppInstanceId(instanceId =>
{
    // Handle the result.
});
@import MRGSFirebase;

NSLog(@"Firebase instance Id: %@", [MRGSFirebase appInstanceID]);
import games.my.mrgs.firebase.MRGSFirebaseAnalytics;
import games.my.mrgs.utils.optional.Consumer;

MRGSFirebaseAnalytics.getInstance().getAppInstanceId(new Consumer<String>() {
    @Override
    public void accept(String instanceId) {
        // Handle the result.
    }
});

Last update: 2023-10-25
Created: 2020-01-20