Search code examples
c#google-apigoogle-analytics-apifirebase-analyticsgoogle-analytics-firebase

Create a web app project that is linked to firebase analytics in C#


I created a web app and wanted to enable the analytics using api in C# library. I implemented it as follows :

    public static void CreateWebProject()
{
        
    var body = new WebApp()
    {
     DisplayName = "mywebapp-fromapi5",
               
    };
    var operationWeb1 = _firebaseManagementService.Projects.WebApps.Create( body, "projects/" + CloudManager.ProjectId).Execute();

    var analytics = new AddGoogleAnalyticsRequest {
                
        AnalyticsAccountId = "248540946"

    };

    var operationWeb2 =  _firebaseManagementService.Projects.AddGoogleAnalytics(analytics, "projects/" + CloudManager.ProjectId).Execute();
    WaitOperation(operationWeb1, nameof(WebApp));

    WaitOperation(operationWeb2, nameof(WebApp));

}

But as I run the code, I get the below error:

Unhandled exception. The service firebase has thrown an exception.
HttpStatusCode is BadRequest.
Google.Apis.Requests.RequestError
Request contains an invalid argument. [400]
Errors [
    Message[Request contains an invalid argument.] Location[ - ] Reason[badRequest] Domain[global]
]

Google.GoogleApiException: The service firebase has thrown an exception. HttpStatusCode is BadRequest. Request contains an invalid argument.
   at Google.Apis.Requests.ClientServiceRequest`1.ParseResponse(HttpResponseMessage response)
   at Google.Apis.Requests.ClientServiceRequest`1.Execute()

 

Solution

  • When the method was first called, it enabled the analytics and linked the Default Account for Firebase for the specific firebase project I used. However, when I tried to link the newly created firebae apps under the same Firebase project by calling the method again, it raised an error, because it is enabled for the firebase project/parent, and there is no need to enable it for the other created firebase apps/child if they are under the same firebase. project.

    To make it clear, I am trying to use .NET libraries of google and firebase to programmatically setup google cloud project, firebase project, firebase web app with analytics enabled.