about google analytics for mobile apps (android or ios) - ios

I am fairly new in Google Analytics in general, so please be patient with me and my questions.
If I am using the Google Analytics for web, by putting in the tracking code in my web header, GA will automatically collect data (visitors, page view, sources, etc)
For mobile app, I need to put the plist (iOS) or json (Android) into the build and compile.
My understanding is that it's not enough just by putting the plist or json file into the app, right?
I would need to implement each and every single thing I want to track. For example, if i want a pageview (screens), then I would need to implement it inside my app code
https://developers.google.com/analytics/devguides/collection/ios/v3/screens
so it's not automatic like in web, where I put the script on header and it works right away.
Is that correct?

I haven't used GA for web, so I can't compare it to GA for mobile.
But I believe you are correct that you need to implement everything you want to track.
It's not too difficult. Using GA 3.11 for iOS, in Swift 3.0, the first thing I do is set up the shared GA instance in my app delegate's didFinishLaunchingWithOptions method:
GAI.sharedInstance().trackUncaughtExceptions = true
GAI.sharedInstance().dispatchInterval = 120
GAI.sharedInstance().logger.logLevel = GAILogLevel.info
GAI.sharedInstance().tracker(withTrackingId: "YOUR GA ID GOES HERE")
To track a screen view, I do this in viewWillAppear of each view controller:
if let tracker = GAI.sharedInstance().defaultTracker {
tracker.set(kGAIScreenName, value: "YOUR SCREEN NAME GOES HERE")
tracker.send(GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject])
}
To send an event:
let tracker = GAI.sharedInstance().defaultTracker
tracker?.send(GAIDictionaryBuilder.createEvent(withCategory: "YOUR CATEGORY", action: "YOUR ACTION", label: "YOUR LABEL", value: NSNumber(integerLiteral: YOURINTEGERVALUE)).build() as NSDictionary as [NSObject : AnyObject])
Setting custom dimensions and sending screen views with dimensions is similar. That's about as far as I've gone with GA.

Related

How to log a custom Event in Facebook Analytics SDK 2020 for swift Documentation is deprecated

UPDATE
The problem is that facebook documentation for swift is outdated so to solve this you will have to log your custom event like this:
func logMyEvent(name : String, value : String) {
let params : [String: Any] = ["myParamName" : "myParamValue"]
let eventName: AppEvents.Name = AppEvents.Name(rawValue: "myEventName")
AppEvents.logEvent(eventName, parameters: params)
}
IMPORTANT!
Take into account that facebook will log your event in its console about 20 minutes after you called it. So do not stress if the data is not there, just wait (I'm talking from experience hahaha). If you have any doubts don't hesitate to contact me, maybe I can help :D
I'm integrating Swift FacebookCore SDK so I can use Facebook Analytics! The problem is that facebooks official documentation DOES NOT WORK! It seems that they haven`t updated the code so I can not get the real code to log my own customized Event!
This is the code that Facebook gives you!
* For more details, please take a look at:
* developers.facebook.com/docs/swift/appevents
*/
func logMyEventEvent(name : String, value : String) {
let params : AppEvent.ParametersDictionary = [
"name" : name,
"value" : value
]
let event = AppEvent(name: "myEvent", parameters: params)
AppEventsLogger.log(event)
}
Got it from here: https://developers.facebook.com/docs/app-events/getting-started-app-events-ios in the Manually Log Events section.
But AppEvent NO LONGER EXISTS.
Searching the web I found out that is because Facebook renamed it to AppEvents
IMAGE WERE I FOUND IT Here is the link to the GitHub poll. https://github.com/facebookarchive/facebook-swift-sdk/issues/433
But this still does not solve my issue, because I can not log a custom event.
Has anyone solved the same problem without going to the previous version?
Thank you very much!
It's hard to believe, but I couldn't find any documentation on this either.
However, I worked out the following code from looking though the FBSDKCoreKit source. I haven't tested it yet, but I am posting it here just in case I meet with a sudden unexpected death in the next few minutes.
import FBSDKCoreKit
let name = "myEvent"
let parameters: [String: Any] = [
"myParameter": "myParameterValue"
]
let event = AppEvents.Name(name)
AppEvents.logEvent(event, parameters: parameters)
For Custom Event you can use
import FBSDKCoreKit
AppEvents.shared.logEvent(AppEvents.Name(rawValue: "AppEventName"), parameters: [AppEvents.ParameterName.init("Key"):"Value"])
import FBSDKCoreKit
AppEvents.logEvent(AppEvents.Name.(*FBSDKAppEventName), parameters: ["*FBEvent": <Any>])
*FBSDKAppEventName based on facebook, so choose one out of these:
Facebook Standart Events
*FBEvent - Based on chosen FBSDKAppEventName there might be, and might not be parameter. Choose one from Parameters and set as String.
Example:
AppEvents.logEvent(AppEvents.Name.achievedLevel, parameters: [AppEvents.Parameters.achieved: "5"])
Example track without the parameters:
AppEvents.logEvent(AppEvents.Name.submitApplication)
Try this:
1- Declare globally an enum with all required events:
enum FACEBOOK_EVENTS: String {
case appOpened = "MyApp iOS is opened"
case appClosed = "MyApp iOS is terminated"
case checkoutClicked = "Checkout in iOS is clicked"
case placeOrderClicked = "Place order in iOS is clicked"
}
2- Second, register manually the event like this:
AppEvents.logEvent(AppEvents.Name.init(rawValue: GlobalClass.FACEBOOK_EVENTS.checkoutClicked.rawValue))
AppEvents.logEvent(AppEvents.Name.init(rawValue: GlobalClass.FACEBOOK_EVENTS.appOpened.rawValue))
...
If you want by default functions:
import FBSDKCoreKit
AppEvents.shared.logEvent(.subscribe)

Messenger not appearing in Share Sheet (UIActivityViewController) of branch.io

I am trying to share something via the branch.io share sheet:
let shareText = "Some Share Text"
let linkProperties = BranchLinkProperties()
linkProperties.feature = "Some"
linkProperties.addControlParam("$desktop_url", withValue: desktopURLString)
linkProperties.addControlParam("$android_url", withValue: androidURLString)
let object = BranchUniversalObject(canonicalIdentifier: "some.cannonical.identifier")
object.title = "Some Title"
object.imageUrl = someImageURL
object.contentDescription = "Some Content Description"
object.addMetadataKey("some_id", value: identifier)
object.showShareSheet(with: linkProperties,
andShareText: shareText,
from: self,
completion: completion)
All works great, except that the Facebook Messenger app does not show as option in the share sheet. Neither in the suggested options nor under 'More'. What is needed to achieve that?
I found the following question / answer for the default UIActivityViewController. How does that work with branch.io though? Facebook Messenger not showing up with UIActivityViewController
When you share via a share sheet on iOS - whether you use the Branch share sheet or UIActivityViewController - the choice of sharing options is not yours to define, it is defined by the user.
The interface for setting which apps will appear on the list can be accessed by opening a Share Sheet and then scrolling through the list of presented apps until you see the "..." (More) option. Tap on this button and you will be presented with the list of apps that can be shared to on the phone:
Each app will have a slider - if the slider is enabled for a particular app, that app will appear in the list.
Enabling Facebook Messenger for sharing on a user's phone is not something you can do from within your app as a developer.
You mention that the Messenger app is not even available when you tap the More button. This strikes me as odd; every device I check does have Facebook Messenger as an option if it is installed. Perhaps try removing and reinstalling Facebook Messenger.
I dived into the issue once more and I finally found the trouble maker. If I set feature of the branch link properties to a string value containing a space, Messenger disappears in the share sheet. The example:
let properties = BranchLinkProperties()
properties.feature = "Share News" //does not work, messenger does not appear in the share sheet
//properties.feature = "Share_News" //works, messenger appears in share sheet
object.showShareSheet(with: properties, andShareText: "Some Share Text", from: viewController, anchor: UIBarButtonItem()) { (activityType, completed) in
if (completed) {
print(String(format: "Branch TestBed: Completed sharing to %#", activityType!))
} else {
print("Branch TestBed: Link Sharing Cancelled\n")
}
}
feature is used as a parameter in the URL in Branch which is then given to the sharing extension. While this, I think, is an encoding issue in Branch, it seems that the Messenger sharing extension is not handling the URL in the same way as other apps. The 'broken url' does work with other sharing extensions. Hope this helps someone else! I will change the name of my feature to something without space for now.

Setting up Google Analytics iOS Swift

I'm trying to setup Google analytics for my app but I do not know how to make a distinction between Production and Testing environments. I also do not how to track a specific user. Are these points even possible?
How I'm currently able to measure if ANYONE lands on a screen in ANY environment is through the following code:
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "LandingViewController")
let builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])
What I want to do is:
- post a user id along with the information logged by GA
- post the APIs base URL that the environment is using back to GA
One way to do this is to send the user id and the api base URL as custom dimensions. See https://support.google.com/analytics/answer/2709828?hl=en
tracker.set(GAIFields.customDimensionForIndex(1), value: userId)
tracker.set(GAIFields.customDimensionForIndex(2), value: baseURL)
Another way to distinguish between Production and Testing is to set up an entirely separate tracking id for Testing - as mentioned by Marta
You should use the attribute trackingId from GAI in order to difference environments.
Using analytics.google.com you need to create a project and inside it you must create two properties. One for test environment and another for production. You will use these ids to difference it.
To post the userId:
let eventTracker: NSObject = GAIDictionaryBuilder.createEvent(
withCategory: "SomeCategory",
action: "SomeAction",
label: "someLabel",
value: userId).build()
tracker.send(eventTracker as [NSObject : AnyObject]!)
Hope it helps

How to set up event tracking with multiple views in google analytics for mobile app (iOS)

Just started using google analytics for my current app.
Let's say i have three kinds of views, for example animals, flowers and birds.
I want to send the id of the object to google analytics respective to the view type when user clicks on that object.
I created these three views(animals, flowers and birds) in my app(Property) in Google Analytics
in each view the code looks like below
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "Birds View")
tracker.send(GAIDictionaryBuilder.createAppView().build())
}
func birdViewClicked(){
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "Birds View")
tracker.send(GAIDictionaryBuilder.createEventWithCategory("Bird", action: "Viewed", label: "Bird123", value: nil).build())
tracker.set(kGAIScreenName, value: nil)
}
So should there be any mapping done between the kGAIScreenName and views in Google Analytics?
Do i need to add filters or goals for the view?
Currently i am getting all objects under "All Mobile App Data" view
Thanks in advance
"All Mobile App Data" is nothing more than the heading Google Analytics uses for your mobile stats. You need to click on this in GA and it will take you to dashboard views. There you can use the defaults or set up your own custom dashboards that show / track the metrics you are interested in. You will see your views listed under 'screens' and your events listed under 'events'. You can also add custom dimensions as per https://developers.google.com/analytics/devguides/collection/ios/v3/customdimsmets

Google Analytics Integration in SWIFT

Im trying to integrate Google Analytics in SWIFT. I used this user guide and tried to do in SWIFt. But I'm having hard time since this is the first time using Google Analytics. Is there any tutorial/resource for SWIFT ? Thanks in advance.
Edit1: Procedure and code I have used,
1. Added the google headers in bridging-header file
2. Added these in Appdelegate
GAI.sharedInstance().trackUncaughtExceptions = true
GAI.sharedInstance().dispatchInterval = 20
GAI.sharedInstance().trackerWithTrackingId("UA-XXXX-YY")
3. Gave the screen name in viewDidAppear as self.screenName = "Game Screen"
4. Created an event as
var tracker = GAI.sharedInstance().trackerWithTrackingId("UA-XXXX-YY")
tracker.send(GAIDictionaryBuilder.createEventWithCategory("SolveGame", action: "GameSolved", label: "Solve", value: nil).build())
I know I'm late, but I was in a similar situation today - not knowing much about Google Analytics and trying to implement it in Swift, for which there's little help online yet. I got it working with basically the same code you have shown here. One suggestion: If you also set
GAI.sharedInstance().logger.logLevel = GAILogLevel.Verbose
You may get some useful messaging in the console.
One other minor point is that I'm calling trackerWithTrackingId() first, before the other calls. Not sure if order matters.
Also, I'm assuming from your point #3 that you're implementing GAITrackedViewController, but figured I would mention that anyway as a tip.
And one final sanity check - you are using your actual tracking id, rather than "UA-XXXX-YY" in your code, right?
I just had to deal with this. I used both the demo provided AND integrated it into my app.
Nothing. 0s.
Then I came in this morning to take a look. It was working this morning. So evidently there is a good bit of lag before this aspect of Google Analytics kicks in.
As for your event tracking, that should work if you are tracking events, however that isn't how you would track a given page.
Assuming that want to track pages to you would want to use something like this.
var storyboardViewName = "Lender-Details-View"
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// [START GOOGLE ANALYTICS]
var tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: storyboardViewName)
var builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])
// [END GOOGLE ANALYTICS]
//.... other code here .....
}

Resources