1

I need to see the sales stats of my Android App in my CRM (in Rails), some people suggest me to use the Google API but I’m confused with all the documentation. Which is the best way to make my CRM an Android Client?

Evelin Ponce
  • 1,056
  • 1
  • 12
  • 31
  • what is confusing you? – petey Jan 22 '18 at 22:27
  • consider reading: https://stackoverflow.com/questions/14140728/getting-statistics-from-google-play-developers-with-an-api – petey Jan 22 '18 at 22:31
  • Sorry @petey I mark this question as Knowledge DB because I'm going to post a quick guide but StackOverflow haven't allowed me to post the answer cause a code format error. – Evelin Ponce Jan 22 '18 at 22:34

1 Answers1

5

1. Upload your app to Google Play Console If you haven’t uploaded your app to Play Store, you can follow the next guide. https://support.google.com/googleplay/android-developer/answer/113469?hl=en

When you create a project, Play Console link your project with a new project at Google Developers Console (Google Play and Google Developers Consoles are different, Play is to manage your apps in Play Store and Developer is to manage the applications and services that you are running in the Google Cloud) so only be sure that your Play Console is linked to a project in Google Cloud visiting in your Google Play Console Dashboard -> Settings(Developer Account) -> API Access -> Linked Project

enter image description here

enter image description here

Check the Linked Project section a and if the button shows UNLINK means Google Play Android Developer it’s ready. If you want to manage your Google Cloud Project since console (For test real-time notifications you need this step) install gcloud on your computer https://cloud.google.com/sdk/downloads

2. Create users and grant permissions to your Rails Project To give access to your rails application you could create a Service Account Credential that is a JSON file you need to set in your application, so every time you need to use the Google API with the Service Account you can create a token that gives you access to the data stored in Google Play. This token expires every 60 minutes so you can restore automatically every 1 hour with a cron or create a method to restore it every time you need access to Google API and the token had expired. (In the example client is working the second option)

Before creating any credential, check if the Cloud Project selected is the one for which you need the credentials, you can check it in the topbar aside Google Logo is the dropdown to select the project you want to work.

enter image description here

Confirming your project, create your Service Account follow this guide: https://developers.google.com/android/management/service-account

enter image description here

Store your JSON in a secure place or in your rails application, only don't forget to gitignore it if you are using git.

Then go to your Google Play Console to Dashboard->Settings->API Access in the Service Accounts section you will see a service with the same name than the one you give to your Service Account in Cloud Console and a blue button with the legend "GRANT ACCESS" press it set the permissions you want to give to that service, if it’s the first time you create services give Administrator permissions only to test, then if you know the limits of your client you can change the permissions of the Service Account in any moment.

After all above, you could pull data from Play Console with code like this

  require 'google/apis/androidpublisher_v2'
  Androidpublisher = Google::Apis::AndroidpublisherV2
  scopes = Androidpublisher::AUTH_ANDROIDPUBLISHER
  authorization = Google::Auth.get_application_default(scopes)
  publisher = Google::Apis::AndroidpublisherV2::AndroidPublisherService.new
  publisher.authorization = authorization
  publisher.authorization.fetch_access_token!
  //Using Android Publisher Api get the details of a product of your app.
  @product_info = publisher.get_in_app_product(params[:package]
  , params[:sku])

As you see in the last code, to get the data of a product you need to require the library AndroidPublisherV2 from Google API, so you need to put in your gemfile this:

gem 'google-API-client', '~> 0.9'

You could check the methods available in the library at http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/AndroidpublisherV2

To learn more about Google Authentication you could visit the next links: https://developers.google.com/identity/protocols/application-default-credentials https://github.com/google/google-auth-library-ruby

3. Configure Google Sub/Pub as webhook If you want to receive real-time notifications when a user acquires a subscription in your Android App using InAppBilling, you will need to use the SUB/PUB Service of Google Cloud Platform. To begin to follow this guide: https://cloud.google.com/pubsub/docs/quickstart-console

After finishing the guide, if you got an error like

ERROR: (gcloud.beta.pubsub.subscriptions.pull) 
NOT_FOUND: Resource not found (resource=realtime-subs).

Probably your gcloud configuration at console is running in a different project, so only use

> gcloud init 

And set default the project where you create the SUB/PUBs.

Then continue with the next tutorial to assign permissions and link your Android App notifications with the SUB/PUB subscription. https://developer.android.com/google/play/billing/realtime_developer_notifications.html

To pull the test notification in your console you can type this in your console:

> gcloud beta pubsub subscriptions pull <subscription-name>

You will identify the notification of Android with the "packageName" value.

Now you need to create a SUB/PUB subscription that will push all the notifications that receive the topic to a callback end-point in your rails application.

First of all, the domain you will use in your callback needs to be registered in Google Domains to assure that you are the owner of that domain. Visit https://console.cloud.google.com/apis/credentials/domainverification And select the tab Add Domain, just follow the steps that the process show you.

enter image description here

If your rails application is in localhost you could use ngrok to assign an URL, only consider that every time you restart ngrok the URL changes and you will need to do the Domain Verification for the new URL.

After that, go to https://console.cloud.google.com/cloudpubsub/ and create a new Subscription with the Delivery Type of Push into an endpoint URL and set your callback URL with https in the domain (even if you haven’t set up https in your domain it will work but avoid this practice if you are in production, please go an set up an SSL in your domain)

enter image description here

enter image description here

Now your Rails Application will get every notification that your app send to your topic in Google Cloud Platform.

A client sample in Sinatra is available as example of ruby client.

Evelin Ponce
  • 1,056
  • 1
  • 12
  • 31