Haddar Macdasi
Haddar Macdasi

Reputation: 3555

can I call google analytics from server side?

I have a web application on it google analytics, doing call on events for example when the user logs in:

 _gaq.push(['_trackEvent', 'webapp', 'account', 'login', , false]);

I have the same web app as a desktop app (one that is on my clients desktops).If user do login from the desktop app it sends request to my web app with params to tell a login was made. so let's say when a user logs in in the desktop app it's sending request to "http://mywebapp.com/?UserID=[userid]&op=login" ,the webapp save the data, and I want it to call google analytics with,

_gaq.push(['_trackEvent', 'desktopapp', 'account', 'login', , false]);

is it possible ? my webapp is MVC 4 C#.

Upvotes: 6

Views: 6748

Answers (1)

tbsalling
tbsalling

Reputation: 4545

Absolutely yes. Your server just needs to form a proper URL string for Google Analytics, such as e.g.:

http://www.google-analytics.com/__utm.gif?utmwv=4.4sj&utmn=2013699399&utmhn=localhost&utmr=-&utmp=%2Fwebapp&utmac=XX-999999-99&utmcc=__utma%3D999.999.999.999.999.1%3B&utmvid=0x10ccd599999999&utmip=

(you need to check and change some of the request parameters to something sensible - read more of the parameters here).

Then open the URL from your server code. In Java you could do it like:

URL url = new URL(urlAsString);
URLConnection connection = url.openConnection();
connection.setUseCaches(false);

connection.addRequestProperty("User-Agent", "MyBrowser/1.0 MyEngine/1.0");
connection.addRequestProperty("Accept-Language", "en-US");

connection.getContent();

This is a similar question: How to trigger Google Analytics from code? for C#.

UPDATE 18 Jul 2014

With the launch of Universal Analytics it's now easier to do server side analytics integration - see the following post here on SA for details: Is there any way to post events to Google Analytics via server-side API?

Upvotes: 7

Related Questions