public static async Task <dynamic> GetAllCampaigns(string companyName, Entities db, HttpClient client) { string alias; try { alias = AliasMethods.GetAlias(companyName, db, "FacebookAdAccountID"); } catch { return(new ArgumentException($"No HubSpot alias found for the company name {companyName}")); } string url = $"{ConfigDictionary.Config()["FacebookAPIURLRoot"]}/{ConfigDictionary.Config()["FacebookAPIVersion"]}/act_{alias}/campaigns?access_token={ConfigDictionary.Config()["FacebookAccessToken"]}"; try { HttpResponseMessage response = await client.GetAsync(url); string responseData = await response.Content.ReadAsStringAsync(); AllCampaignsRoot allCampaignsRoot = JsonConvert.DeserializeObject <AllCampaignsRoot>(responseData); return(allCampaignsRoot); } catch (HttpRequestException ex) { return(ex); } }
public static async Task <dynamic> GetEngagements(HttpClient client, string companyName, Entities db, string companyID) { string alias; try { alias = AliasMethods.GetAlias(companyName, db, "HubSpot"); } catch { return(new ArgumentException($"No HubSpot alias found for the company name {companyName}")); } string uri = $"https://api.hubapi.com/engagements/v1/engagements/associated/COMPANY/{companyID}/paged?limit=100&hapikey=" + ConfigDictionary.Config()["HubSpotAPIKey"]; try { HttpResponseMessage response = await client.GetAsync(uri); string responseData = await response.Content.ReadAsStringAsync(); EngagementsRoot engagementsData = JsonConvert.DeserializeObject <EngagementsRoot>(responseData); while (engagementsData.HasMore) { engagementsData = await ChainedEngagementsRequest(engagementsData, companyID, client); } return(engagementsData); } catch (HttpRequestException ex) { return(ex); } }
public async static Task <dynamic> ImpressionsYesterday(List <string> companyNames) { var dailyImpressionsDTO = new DailyImpressionsDTO(); dailyImpressionsDTO.DailyImpressionsSummary = new List <CompanyDailyImpressionsSummary>(); foreach (string company in companyNames) { string customerID; try { using (Entities db = new Entities()) { customerID = AliasMethods.GetAlias(company, db, "GoogleAdsCustomerID"); } } catch { return(new ArgumentException($"No ID found for the company { company }")); } try { GoogleAdsClient client = new GoogleAdsClient(); // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsService = client.GetService( Services.V4.GoogleAdsService); string dailyImpressionsQuery = "SELECT " + "campaign.id, " + "campaign.name, " + "metrics.impressions " + "FROM campaign " + "WHERE campaign.serving_status='SERVING' " + "AND segments.date DURING YESTERDAY"; RepeatedField <GoogleAdsRow> dailyImpressionsResults = RequestMethods.SearchRequest(customerID, dailyImpressionsQuery, googleAdsService); if (dailyImpressionsResults.Any(x => x.Metrics.Impressions > 0 && x.Metrics.Impressions < 100)) { dailyImpressionsDTO.DailyImpressionsSummary.Add(new CompanyDailyImpressionsSummary(company, dailyImpressionsResults)); } } catch (GoogleAdsException e) { Debug.WriteLine("Failure:"); Debug.WriteLine($"Message: {e.Message}"); Debug.WriteLine($"Failure: {e.Failure}"); Debug.WriteLine($"Request ID: {e.RequestId}"); } } return(await SendEmailAlert(dailyImpressionsDTO)); }
public static async Task <dynamic> GetHubSpotInfo(HttpClient client, Entities db, string companyName, string[] searchProps) { string alias; try { alias = AliasMethods.GetAlias(companyName, db, "HubSpot"); } catch { return(new ArgumentException($"No HubSpot alias found for the company name {companyName}")); } List <Filter> filters = new List <Filter>(); Filter nameFilter = new Filter("name", "EQ", alias); filters.Add(nameFilter); RequestBody search = new RequestBody(filters, searchProps.ToList()); string searchJson = JsonConvert.SerializeObject(search); StringContent searchData = new StringContent(searchJson, Encoding.UTF8, "application/json"); string uri = $"{ConfigDictionary.Config()["HubSpotAPIURLRoot"]}/companies/search?hapikey={ConfigDictionary.Config()["HubSpotAPIKey"]}"; try { HttpResponseMessage response = await client.PostAsync(uri, searchData); string responseData = await response.Content.ReadAsStringAsync(); CompanyRoot.Company companyData = JsonConvert.DeserializeObject <CompanyRoot.Company>(responseData); return(companyData); } catch (HttpRequestException ex) { return(ex); } }
public async Task <dynamic> GetProjectID(string companyName, HttpClient client, Entities db) { try { string alias = AliasMethods.GetAlias(companyName, db, "BugHerd"); string uri = $"{ConfigDictionary.Config()["BugHerdURLRoot"]}/projects/active.json"; HttpResponseMessage response = await client.GetAsync(uri); string responseData = await response.Content.ReadAsStringAsync(); ProjectsRoot allProjects = JsonConvert.DeserializeObject <ProjectsRoot>(responseData); return(allProjects.Projects.FirstOrDefault(x => x.Name == alias).ID); } catch { return(null); } }
public static async Task <dynamic> GetProject(string companyName, HttpClient client, Entities db) { try { string alias = AliasMethods.GetAlias(companyName, db, "Everhour"); string uri = ConfigDictionary.Config()["EverHourURLRoot"] + $"/projects?query={alias}"; HttpResponseMessage response = await client.GetAsync(uri); string responseData = await response.Content.ReadAsStringAsync(); ProjectRoot projectRoot = JsonConvert.DeserializeObject <List <ProjectRoot> >(responseData).FirstOrDefault(); return(new ProjectDTO(projectRoot)); } catch (Exception ex) { return(ex); } }
public static async Task <Queue <string> > ReturnFolderID(HttpClient client, string companyName, Entities db) { try { string alias = AliasMethods.GetAlias(companyName, db, "ClickUp"); List <Folder> allFolders = await GetAllFolders(client); try { return(GetFolderID(allFolders, alias)); } catch { throw; } } catch { return(null); } }
public dynamic Get(string companyName) { if (CheckClientSecret()) { string customerID; try { using (Entities db = new Entities()) { customerID = AliasMethods.GetAlias(companyName, db, "GoogleAdsCustomerID"); } } catch { return(new ArgumentException($"No ID found for the company {companyName}")); } GoogleAdsClient client = new GoogleAdsClient(); // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsService = client.GetService(Services.V4.GoogleAdsService); string query = "SELECT " + "account_budget.approved_spending_limit_micros, " + "account_budget.proposed_spending_limit_micros, " + "account_budget.approved_start_date_time, " + "account_budget.proposed_start_date_time, " + "account_budget.approved_end_date_time, " + "account_budget.proposed_end_date_time, " + "account_budget.amount_served_micros " + "FROM account_budget"; try { RepeatedField <GoogleAdsRow> results = RequestMethods.SearchRequest(customerID, query, googleAdsService); if (results.Any()) { List <GoogleAdsBudgetDTO> resultsDTO = new List <GoogleAdsBudgetDTO>(); for (int i = 0; i < results.Count; i++) { resultsDTO.Add(new GoogleAdsBudgetDTO(results[i])); } return(resultsDTO); } else { return(JsonConvert.SerializeObject("No results were found")); } } catch (GoogleAdsException e) { Debug.WriteLine("Failure:"); Debug.WriteLine($"Message: {e.Message}"); Debug.WriteLine($"Failure: {e.Failure}"); Debug.WriteLine($"Request ID: {e.RequestId}"); throw; } } else { return(new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)); } }
public dynamic Get(string companyName) { if (CheckClientSecret()) { string customerID; try { using (Entities db = new Entities()) { customerID = AliasMethods.GetAlias(companyName, db, "GoogleAdsCustomerID"); } } catch { return(new ArgumentException($"No ID found for the company {companyName}")); } GoogleAdsClient client = new GoogleAdsClient(); // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsService = client.GetService( Services.V4.GoogleAdsService); string query = "SELECT " + "campaign.id, " + "campaign.name, " + "campaign.start_date, " + "campaign.end_date, " + "metrics.impressions, " + "metrics.clicks, " + "metrics.conversions_value, " + "metrics.conversions, " + "metrics.cost_micros " + "FROM campaign " + "WHERE campaign.serving_status='SERVING' " + "AND segments.date DURING THIS_MONTH " + "ORDER BY metrics.impressions DESC"; string lastMonthQuery = "SELECT " + "metrics.conversions_value, " + "metrics.conversions," + "metrics.cost_micros " + "FROM campaign " + "WHERE segments.date DURING LAST_MONTH "; try { RepeatedField <GoogleAdsRow> results = RequestMethods.SearchRequest(customerID, query, googleAdsService); if (results.Any()) { List <GoogleAdsCampaignDTO> campaignsDTO = new List <GoogleAdsCampaignDTO>(); double?totalSpend = 0; double?totalValue = 0; double?totalConversions = 0; double?rOAS = 0; for (int i = 0; i < results.Count; i++) { campaignsDTO.Add(new GoogleAdsCampaignDTO(results[i])); totalSpend += results[i].Metrics.CostMicros; totalValue += results[i].Metrics.ConversionsValue; totalConversions += results[i].Metrics.Conversions; } double?totalSpendPounds = totalSpend / 1000000; if (totalValue != 0 && totalSpendPounds != 0) { rOAS = totalValue / totalSpendPounds; } RepeatedField <GoogleAdsRow> lastMonthResults = RequestMethods.SearchRequest(customerID, lastMonthQuery, googleAdsService); if (lastMonthResults.Any()) { double?lMTotalSpend = 0; double?lMTotalValue = 0; double?lMTotalConversions = 0; double?lMROAS = 0; for (int i = 0; i < lastMonthResults.Count; i++) { lMTotalSpend += lastMonthResults[i].Metrics.CostMicros; lMTotalValue += lastMonthResults[i].Metrics.ConversionsValue; lMTotalConversions += lastMonthResults[i].Metrics.Conversions; } double?lMTotalSpendPounds = lMTotalSpend / 1000000; if (lMTotalValue != 0 && lMTotalSpendPounds != 0) { lMROAS = lMTotalValue / lMTotalSpendPounds; } return(new GoogleAdsCampaignSummaryDTO(campaignsDTO, totalSpendPounds, totalConversions, totalValue, rOAS, lMROAS)); } else { return(new GoogleAdsCampaignSummaryDTO(campaignsDTO, totalSpend / 1000000, totalConversions, totalValue, rOAS, 0)); } } else { return(JsonConvert.SerializeObject("No results were found")); } } catch (GoogleAdsException e) { Debug.WriteLine("Failure:"); Debug.WriteLine($"Message: {e.Message}"); Debug.WriteLine($"Failure: {e.Failure}"); Debug.WriteLine($"Request ID: {e.RequestId}"); throw; } } else { return(new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)); } }