Skip to content
Advertisement

How to add the data to Google Analytis by using java (programmatically)

At the current moment, I am trying to understand how to add any data to GA. I read the data from my GA account using Core Reporting API and Managment API without any problems. But now I want to add the data (the number of phone calls) to GA account programmatically. Somebody can explain me step by step – how can I do this?

Advertisement

Answer

The Measurement Protocol is how we send data to Google Analytics. The JavaScript snippet that we use in our websites also uses the Measurement protocol as do the SDK’s for Android and IOS. Unfortunately there is no (official) SDKs for the other languages like Java for instance.

That being said you can technically code it yourself in any language that can handle a HTTP get or a HTTP post. I have personally done it for C#.

POST /collect HTTP/1.1
Host: www.google-analytics.com

payload_data


The following parameters are required for each payload:
v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.
&t=              // Hit Type.

A few tips to get you started.

  1. Check out validating hits this is very useful in the beginning for debugging your requests.
  2. some of the parameters are only valid for certain hit types. Make sure you check the documentation.
  3. Cid is just a string it can be anything most people send a Guid its basically used by the server to identify a unique session.
  4. if you are doing this for an application google analytics account remember to send screenview not pageview the same goes for web application.
  5. check the realtime report on google analytics to see if your hits are getting recorded.

Update for question in comment:

I recommend while you are getting the idea of this you start with just using HTTP GET in a web browser. Its easer to test your requests against debug that way. For example put this in a browser.

https://google-analytics.com/debug/collect?v=1&tid=UA-123456-1&cid=5555&t=pageview&dp=%2FpageA

DP is document path and I am not sure why it is requiring that you send that.

ScreenView hit type VS PageView hit type.

There are two types of Google Analytics accounts ones for applications like android applications or sometimes web applications, and web sites. Application Google analytics accounts are meant to be used with ScreenView hit (the user checks a screen in the application) type and web accounts use PageViews (the user views a webpage). If you send a Pageview to an application Google Analytics account it will accept the hit but there will be no way for you to see the data. If you send a ScreenView to a web Google Analytics account it will again accept the data but you wont see it.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement