Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route Google Analytics v3 API through a proxy server

We are using Google Analytics API v3 (dot net version) for reporting some statistical data on our website. I have the code running fine on my local machine, but it wouldn't work on the production server due to some firewall rules. Our system admin suggests to try and use a proxy. I searched on the internet for any guidelines to set up proxy for Google Analytics API service, but with no luck. Appreciate any pointers in this regard.

EDIT:

   public DataTable GetSearchTrends()
    {
        string GoogleAnalyticsProfileId = AppConfigManager.GetGoogleAnalyticsProfileIdForInis();

        var service = new AnalyticsService(new BaseClientService.Initializer()
        {
            Authenticator = Authenticate()

        });

            DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
            GoogleAnalyticsProfileId,
            string.Format("{0:yyyy-MM-dd}", StartDate),
            string.Format("{0:yyyy-MM-dd}", EndDate),
            GoogleAnalyticsSearchUniquesMetric
            );

        request.Dimensions = GoogleAnalyticsSearchKeywordMetric;
        request.Sort = string.Concat("-", GoogleAnalyticsSearchUniquesMetric);
        request.MaxResults = NumberOfSearchTrendsToFetch;

        GaData response = request.Fetch();

        return SearchTrendsHelper.ConvertToDataTable(
            response.Rows,
            SearchTrendsKeywordsExcludeList,
            NumberOfSearchTrendsToDisplay
            );
    }


   private IAuthenticator Authenticate()
    {
        string GoogleAnalyticsServiceScope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue();
        string GoogleApiServiceAccountId = AppConfigManager.GetGoogleApiServiceAccountId();
        string GoogleApiServiceAccountKeyFile = AppConfigManager.GetGoogleApiServiceAccountKeyFile();
        string GoogleApiServiceAccountKeyPassword = AppConfigManager.GetGoogleApiServiceAccountKeyPassword();
        AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;

        X509Certificate2 key = new X509Certificate2(
            HttpContextFactory.Current.Server.MapPath(GoogleApiServiceAccountKeyFile), 
            GoogleApiServiceAccountKeyPassword,
            X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet
            );

        AssertionFlowClient client = new AssertionFlowClient(desc, key) { 
            ServiceAccountId = GoogleApiServiceAccountId, 
            Scope = GoogleAnalyticsServiceScope,
        };

        OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(
            client,
            AssertionFlowClient.GetState
            );

        return auth;
    }
like image 832
itsbalur Avatar asked May 03 '13 09:05

itsbalur


1 Answers

I did not find any useful documentation in the forums or on the internet, so decided to use the System.Net configuration on the web.config.

 <system.net>
    <defaultProxy useDefaultCredentials="true">
      <proxy proxyaddress="http://abc.com:3128" usesystemdefault="True" bypassonlocal="True"/>
      <bypasslist>
        <add address="http://xyz.com" />
        <add address="http://www.example.com" />
      </bypasslist>
    </defaultProxy>
  </system.net>

Any requests we don't want to pass through the proxy, can be added to the <bypasslist>. It has the added advantage that, whenever the Google API Class library changes, we don't have to bother about re-writing code to set up the proxy. :-)

like image 104
itsbalur Avatar answered Sep 20 '22 21:09

itsbalur