Google Analytics Integration in SWIFT - ios

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 .....
}

Related

Game Center Matchmaking Custom Invites

I'm sure theres a 'simple' answer to this, but I've been having a difficult time finding it.
I've created an iOS card game that works in game center. At the moment, it determines the online players automatically and works fine.
What I want to do is have the user invite their game center friends to play. The documentation isn't really helping me.
I'm doing my project in swift, and I feel like I'm dealing with deprecated items and old documentation here?
I've managed to get the users list of friends and then I assume I need to instantiate a matchmaker with that list of friends.
What I'm stuck at, is trying to implement the "RecipientResponseHandler" code as per apple's guidelines:
request.recipientResponseHandler = ^(GKPlayer *player, GKInviteeResponse response)
{
[self updateUIForPlayer: player accepted: (response == GKInviteeResponseAccepted)];
};
Question 1: First of all, What does the ^ symbol represent in the swift language?
Question 2: XCode doesn't seem to like the * symbol in the arguments...I've tried doing things like...
request.recipientResponseHandler = (player: GKPlayer, response: GKInviteeResponse){
print("Do codeStuffHere")
};
But, I can't find a way of wording this that XCode will allow...
Question 3: How can I write this response handler to get it to work?
Question 4: Also,to listen for invites, I need to implement the GKLocalPlayerListener protocol...Anything else?
Question 5: I feel like I'm missing something glaring?
I've been learning on my own, and sometimes it takes a while to understand concepts without any direction, so please go easy on me internet. I'm just starting to understand completion handlers and closures...
Thanks in advance for any help.
What does the ^ symbol represent in the swift language?
XCode doesn't seem to like the * symbol in the arguments.
That's because that code is written in Objective-C, not Swift.
I'm assuming you're looking at the documentation for GKMatchRequest (update as of Jan 2023: It seems Apple has removed the code listing from this page at some point).
If so, then yeah, Apple just hasn't gotten around to updating all their documentation to use Swift instead of Objective-C (even on pages where the language selected is Swift, like this one).
Here's the Swift equivalent of that entire code listing:
func invite(friends: [GKPlayer]) {
let request = GKMatchRequest()
request.minPlayers = 2
request.maxPlayers = 4
request.recipients = friends
request.inviteMessage = "Your Custom Invitation Message Here"
request.recipientResponseHandler = { player, response in
self.updateUI(for: player, accepted: response == .accepted)
}
}
func updateUI(for player: GKPlayer, accepted: Bool) {
// update your UI here
}
For question 4, be sure to register the GKLocalPlayerListener and only register once, for example:
GKLocalPlayer.local.register(self)

How to get full list of address components with Google Maps iOS SDK

Good morning SO!
I am currently working on an iOS app that requires me to use Google Maps' reverse geocoding features. The tricky part is that I need to be able to access the full list of address components (i.e. the full "address_components" value from the JSON returned from this example, including short and long names)
When using the Google Maps iOS SDK, it seems that all results from reverse geocoding are given in the form of GMSAddress, which only exposes a small subset of these values (and no short versions). I have so far not found a way to access what I need.
Is there any way to get these values through the iOS SDK? If not, is it okay to try and call the google maps API directly? Has anybody tried?
Thank you for your time,
Julien P.
I am working on one such app...I don't know about iOS SDK.
But, Google API will help for this. This tutorial here gets the address of a location you click on:
https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial
You will not be able to call Google API directly. You will need to add a server for this. This tutorial gives a detailed and clear way to do this.
Hope this helps. All the best
I found an easy solution using the LMGeocoder library for iOS.
Inside LMAddress, there is a lines attribute that will contain all the information. You can access it easily with the following piece of code.
func getAddressComponent(name: String, lines: [[NSObject: AnyObject?]]) -> (short: String?, long: String?)? {
let filteredLines = lines.filter { ($0["types"] as! [String]).contains(name)
guard let line = filteredLines.first else {
return nil
}
let longName = line["long_name"] as? String
let shortName = line["short_name"] as? String
return (short: shortName, long: longName)
}
This is using Swift 2.3 but will probably work for Swift 3 as well. Hope it helps anyone! :)
Julien

Trying to set Estimote iBeacon GPIO pin .high - SWIFT iOS

Ive been trying for a few days now to set a pin high (Estimote location beacon) from an app I'm building.
Im doing something wrong as i am getting an error when the block fires off. Error is: [ESTTelemetryInfo portsData]: unrecognized selector sent to instance...
Ive looked everywhere for a snippet but can't find anything. I only want to be able to set the pin high (i don't need to send any data). If i can set the pin high i figure i could set it low when done using the same methods. This is the code:
let telem = ESTTelemetryInfo.init(shortIdentifier: "xxxxxxxxxxxxxxxx")!
let setPinHigh = ESTTelemetryNotificationGPIO.init(notificationBlock: { (telemInfo) in
if telInfo.shortIdentifier! != "xxxxxxxxxxxxxxxx" { return }
telemInfo.portsData.setPort(.port0, value: .high)
})
setPinHigh.fireNotificationBlock(with: telem)
Any help would be greatly appreciated.
ps Sorry if this is incorrectly formatted (long time reader first time poster).
Cheers
Gary
Fixed..we'll sort of. For anyone wanting to know the right way to to set a pin high, in output mode, is to connect to the beacon first through the device manager: ESTDeviceManager() -set the delegate in the class as ESTDeviceManagerDelegate - startDeviceDiscovery(with: deviceFilter) then in the delegate method:
func estDeviceConnectDidSucceed(_ device: ESTDeviceConnectable) { self.settings.gpio.portsData.setPort(.port0, value: .high)
}
BUT -> at the moment there is a bug that portsData has no member 'setPort'. I've filed a bug issue with Estimote on GitHub. Will come back to report once it's fixed.

about google analytics for mobile apps (android or 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.

.or query in parse unity crashes on iOS

I'm doing a .or query which in every platform works fine except in iOS.
As soon it reaches the line where it does the .or, it crashes with:
System.Collections.Generic.List> doesn't implement interface System.Collections.Generic.IEnumerable>
Assertion: should not be reached at mini-trampolines.c:183
Here's the code:
var isChallenger = ParseObject.GetQuery("Match")
.WhereEqualTo("Challenger",fb.loggedUser);
var isChallenged = ParseObject.GetQuery("Match")
.WhereEqualTo("Challenged",fb.loggedUser);
ParseQuery<ParseObject> query = isChallenger.Or (isChallenged); // Crashes here.
I'm doing it just like in the docs, not sure what's wrong.
Any help would be much appreciated!
Thanks,
Pablo
I think this is a bug I reported here: https://developers.facebook.com/bugs/750897958275392/
Except, I only saw this bug when using ParseQuery. The workaround I used is to use ParseQuery only, but I see you're already doing that. Is the code you posted exactly the same as the code you're actually using?

Resources