20.12.31 GoogleAnalytics API 도입 테스트(feat.Maven)

2020. 12. 31. 17:31JAVA/Blog 사이트 프로젝트

<testApp.java>

public class testApp {

	@JsonIgnoreProperties(ignoreUnknown = true)
	public static class TestDataType1 {
		public int age;
		public String name;
		public int height;
	}

	public void run() {
		 //testApp();

		//testJackson5();
		
		testGoogleCredentials();
		testUpdateGoogleAnalyticsApi();
	}

	private void testUpdateGoogleAnalyticsApi() {
		//GoogleAnalytics 버전4의 PropertyId 가져오기
		String ga4PropertyId = Container.appConfig.getGa4PropertyId();

	    try (AlphaAnalyticsDataClient analyticsData = AlphaAnalyticsDataClient.create()) {
	      RunReportRequest request = RunReportRequest.newBuilder()
	          .setEntity(Entity.newBuilder().setPropertyId(ga4PropertyId)) //검색(데이터 가져올) 대상
	          //해설 참고: http://www.goldenplanet.co.kr/blog/2017/01/24/google-analytics-dimension-metric/
	          //공식 정보: https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema
	          //Dimension(측정 기준): 웹사이트 방문자들의 특성(속성)
	          .addDimensions(
	              Dimension.newBuilder().setName("pagePath")) //pagePath: The web pages visited, listed by URI.
	          //Metric(측정 항목): Dimension을 측정하는 “숫자”
	          .addMetrics(Metric.newBuilder().setName("activeUsers")) //activeUsers: The number of distinct users who visited your site or app.
	          .addDateRanges(
	              DateRange.newBuilder().setStartDate("2020-10-01").setEndDate("today")).build();

	      // Make the request
	      RunReportResponse response = analyticsData.runReport(request);

	      System.out.println("Report result:");
	      for (Row row : response.getRowsList()) {
	        System.out.printf("%s, %s%n", row.getDimensionValues(0).getValue(),
	            row.getMetricValues(0).getValue());
	      }
	    } catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	private void testGoogleCredentials() {
		//환경변수 경로를 가져오기
		String variablesPath = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
		System.out.println(variablesPath);
		
		
	}