0
votes

I am trying to update custom dimension fields(https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/customDimensions/update) in google analytics by calling the analytics api from C#.

I created a project in https://console.developers.google.com, added a service account(downloaded the .p12, private key file),enabled the analytics api and linked the service account email in https://analytics.google.com

I am able to read the "analytics data"(like account summaries etc) but not insert or update. When I try to do that, I get the Insufficient permission 403 error. The service account added to google analytics has all the privileges.

 class Program
    {
        static void Main(string[] args)
        {
            test();
        }

        public static void test() //takes clientid as input
        {

            string[] scopes = new string[] { AnalyticsService.Scope.Analytics }; // view and manage your Google Analytics data

            var keyFilePath = @"C:\Users\xyz\Desktop\CustomDimUpdate\xxxxxxxx.p12";    // Downloaded from https://console.developers.google.com
            var serviceAccountEmail = "xxxx.iam.gserviceaccount.com";  // found https://console.developers.google.com

            //loading the Key file
            var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = scopes
            }.FromCertificate(certificate));


            var service = new AnalyticsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
                //ApplicationName = "Analytics API Sample",
            });


            CustomDimension body = new CustomDimension();
            body.Name = "Configurable"; //Found in https://analytics.google.com
            body.Scope = "Product"; //Found in https://analytics.google.com
            body.Active = true;


            try
            {
                //analytics.management().customDimensions()
                //    .update("123456", "UA-123456-1", "ga:dimension2", body).execute();
                ManagementResource.CustomDimensionsResource.UpdateRequest update = service.Management.CustomDimensions.Update(body, "123456", "UA-123456-1", "ga:dimension1");

                update.Execute(); //Errors out here

                ManagementResource.AccountsResource.ListRequest list = service.Management.Accounts.List();
                list.MaxResults = 1000; // Maximum number of Accounts to return, per request. 
                Accounts feed1 = list.Execute(); //Works perfectly fine

                foreach (Account account in feed1.Items)
                {
                    // Account
                    Console.WriteLine(string.Format("Account: {0}({1})", account.Name, account.Id));
                }

                ManagementResource.ProfilesResource.ListRequest list1 = service.Management.Profiles.List("123456", "UA-123456-1");
                Profiles feed = list1.Execute();
                foreach (Profile profile in feed.Items)
                {
                    Console.WriteLine(string.Format("\t\tProfile: {0}({1})", profile.Name, profile.Id));
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

            }


        }
1

1 Answers

1
votes

I was able to resolve this by changing the below line of code

string[] scopes = new string[] { AnalyticsService.Scope.Analytics }; // view and manage your Google Analytics data

to

string[] scopes = new string[] { AnalyticsService.Scope.AnalyticsEdit }; // view and manage your Google Analytics data