{"id":1775,"date":"2020-10-04T13:05:51","date_gmt":"2020-10-04T13:05:51","guid":{"rendered":"https:\/\/sezeromer.com\/?p=1775"},"modified":"2023-02-27T22:49:58","modified_gmt":"2023-02-27T19:49:58","slug":"xamarin-forms-firebase-analytics","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/xamarin-forms-firebase-analytics\/","title":{"rendered":"Xamarin Forms Firebase Analytics"},"content":{"rendered":"<p>Hello friends. In this article, we will talk about how to send events with Firebase in Xamarin Forms and how to debug these events. First, we need to open a project on a Firebase. Later, we will create 2 different applications from this project. One of them will be for iOS and the other for Android.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1851\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-10.56.12-1024x563.png\" sizes=\"(max-width: 640px) 100vw, 640px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-10.56.12-1024x563.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-10.56.12-300x165.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-10.56.12-768x422.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-10.56.12-1536x844.png 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-10.56.12-2048x1125.png 2048w\" alt=\"\" width=\"640\" height=\"352\" \/><\/p>\n<p>Make sure that the bundle id is correct while creating an application in the project. We need to download and add the files google-services.json for Android and GoogleServiceInfo.plist for iOS, which are the last step of creating an application. The point we need to pay attention here is; After adding the json file on the Android side, we need to specify that it is a GoogleServicesJson in the BuildAction section. If this option doesn&#8217;t come, VS For Mac is totally absurd. You need to edit the csproj file on the android side of the project.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1852\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.02.23.png\" sizes=\"(max-width: 651px) 100vw, 651px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.02.23.png 651w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.02.23-206x300.png 206w\" alt=\"\" width=\"651\" height=\"949\" \/><\/p>\n<p>When you open your csproj file with any editor, you will probably see that your google-services.json file is None. Add this itself as GoogleServicesJson and you will see that it is fixed when you run your project again.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1853\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.06.39-1024x518.png\" sizes=\"(max-width: 640px) 100vw, 640px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.06.39-1024x518.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.06.39-300x152.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.06.39-768x388.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.06.39-1536x776.png 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.06.39-2048x1035.png 2048w\" alt=\"\" width=\"640\" height=\"324\" \/><\/p>\n<p>Then, on the iOS side, we must make sure that the Google-Services-Info.plist file we added is set as BundleResource from the Build Action option.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1856\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-16.00.51.png\" sizes=\"(max-width: 549px) 100vw, 549px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-16.00.51.png 549w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-16.00.51-300x226.png 300w\" alt=\"\" width=\"549\" height=\"413\" \/><\/p>\n<p>Now we can come to the Xamarin side. We will proceed through DependencyService as we will do different operations for each platform here. If you want to know more about what DependencyService is, there is a more detailed explanation here. First of all, we design an interface for what functions we will use in this service. Since I will only log one event, this interface will do the trick. You can customize it according to your needs.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">using System;\r\nnamespace XamFirebaseEvents.DependencyServices\r\n{\r\n    public interface IFirebaseEvents\r\n    {\r\n        void LogEvent(string Id, string name, string value);\r\n    }\r\n}<\/pre>\n<\/div>\n<p>After that, I first switch to the Android side. Here we need to install packages. These packages;<\/p>\n<ul>\n<li>Xamarin.FireBase.Analytics<\/li>\n<li>Xamarin.FireBase.Analytics.Impl<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1854\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.16.01-1024x550.png\" sizes=\"(max-width: 640px) 100vw, 640px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.16.01-1024x550.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.16.01-300x161.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.16.01-768x412.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.16.01-1536x824.png 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.16.01.png 1988w\" alt=\"\" width=\"640\" height=\"344\" \/><\/p>\n<p>After installing the packages, I create FirebaseEvents class on the Android layer and inherit from IFirebaseEvents. I can log events thanks to the packages I added later. The part you need to pay attention to here is that when it tries to get an instance from FirebaseAnalytics, it requests an activity from you. The problem will be solved if you set the MainActivity itself of the onCreate function after defining a static variable on the MainActivity.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">using System;\r\nusing Android.OS;\r\nusing Firebase.Analytics;\r\nusing XamFirebaseEvents.DependencyServices;\r\n[assembly: Xamarin.Forms.Dependency(typeof(IFirebaseEvents))]\r\nnamespace XamFirebaseEvents.Droid.DependencyServices\r\n{\r\n    public class FirebaseEvents : IFirebaseEvents\r\n    {\r\n        public void LogEvent(string Id, string name, string value)\r\n        {\r\n            var firebaseAnalytics = FirebaseAnalytics.GetInstance(MainActivity.activity);\r\n            var bundle = new Bundle();\r\n            bundle.PutString(name, value);\r\n\r\n            firebaseAnalytics.LogEvent(Id, bundle);\r\n        }\r\n    }\r\n}<\/pre>\n<\/div>\n<p>The next step is the iOS part. Here again, as we did in Android, we create a new class; We ensure that this class inherits from the interface we created. After that, we need to add the Xamarin.Firebase.iOS.Analytics package.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1855\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.30.14-1024x550.png\" sizes=\"(max-width: 640px) 100vw, 640px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.30.14-1024x550.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.30.14-300x161.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.30.14-768x412.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.30.14-1536x824.png 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-10-04-at-15.30.14.png 1988w\" alt=\"\" width=\"640\" height=\"344\" \/><\/p>\n<p>After adding the package, we need to add these codes to the class we created.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">using System;\r\nusing System.Collections.Generic;\r\nusing Firebase.Analytics;\r\nusing Foundation;\r\nusing XamFirebaseEvents.DependencyServices;\r\n[assembly: Xamarin.Forms.Dependency(typeof(IFirebaseEvents))]\r\nnamespace XamFirebaseEvents.iOS.DependencyServices\r\n{\r\n    public class FirebaseEvents : IFirebaseEvents\r\n    {\r\n        public void LogEvent(string Id, string name, string value)\r\n        {\r\n            var keys = new List&lt;NSString&gt;()\r\n            {\r\n                new NSString(name)\r\n            };\r\n            var values = new List&lt;NSString&gt;()\r\n            {\r\n                new NSString(value)\r\n            };\r\n            Analytics.LogEvent(Id, NSDictionary&lt;NSString, NSObject&gt;.FromObjectsAndKeys(values.ToArray(), keys.ToArray(), keys.Count));\r\n        }\r\n    }\r\n}<\/pre>\n<\/div>\n<p>The last action will be on AppDelegate. Here it is necessary to configure Firebase.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\n\r\nusing Foundation;\r\nusing UIKit;\r\n\r\nnamespace XamFirebaseEvents.iOS\r\n{\r\n    \/\/ The UIApplicationDelegate for the application. This class is responsible for launching the \r\n    \/\/ User Interface of the application, as well as listening (and optionally responding) to \r\n    \/\/ application events from iOS.\r\n    [Register(\"AppDelegate\")]\r\n    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate\r\n    {\r\n        public override bool FinishedLaunching(UIApplication app, NSDictionary options)\r\n        {\r\n            global::Xamarin.Forms.Forms.Init();\r\n            LoadApplication(new App());\r\n\r\n            Firebase.Core.App.Configure();\r\n\r\n            return base.FinishedLaunching(app, options);\r\n        }\r\n    }\r\n}<\/pre>\n<\/div>\n<p>The point we need to pay attention here is; The events we log are on the console the next day. You can debug while testing events. For this, it will be useful to check the next article.<\/p>\n<p>You can ask the places you want to ask by sending e-mail or comment. Good work.<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends. In this article, we will talk about how to send events with Firebase in Xamarin Forms and how to debug these events. First, we need to open a project on a Firebase. Later, we will create 2 different applications from this project. One of them will be for iOS and the other for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1857,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[289,1,288,3],"tags":[114,436,730,728,293,726,725,731,530,115,727,7,729,22,119],"class_list":["post-1775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-c-ile-ilgili-yazilar","category-ios","category-xamarin-ile-yapilmis-projeler","tag-android","tag-debug","tag-debug-evetn","tag-dependency","tag-dependency-service","tag-event","tag-firebase","tag-firebase-events","tag-form","tag-ios","tag-log","tag-omer-sezer","tag-services","tag-xamarin","tag-xamarin-forms"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1775"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=1775"}],"version-history":[{"count":7,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1775\/revisions"}],"predecessor-version":[{"id":2653,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1775\/revisions\/2653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/1857"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=1775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=1775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=1775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}