AnalyticsReportsDataProvider { public List<TrafficSource> getTrafficSources(long companyId, String url) throws PortalException { try { String response = _asahFaroBackendClient.doGet( companyId, "api/seo/1.0/traffic-sources?url=" + url); TypeFactory typeFactory = _objectMapper.getTypeFactory(); return _objectMapper.readValue( response, typeFactory.constructCollectionType( List.class, TrafficSource.class)); } catch (Exception exception) { throw new PortalException( "Unable to get traffic sources", exception); } } AnalyticsReportsDataProvider(Http http); HistoricalMetric getHistoricalReadsHistoricalMetric(
			long companyId, TimeRange timeRange, String url); HistoricalMetric getHistoricalViewsHistoricalMetric(
			long companyId, TimeRange timeRange, String url); Long getTotalReads(long companyId, String url); Long getTotalViews(long companyId, String url); List<TrafficSource> getTrafficSources(long companyId, String url); boolean isValidAnalyticsConnection(long companyId);  }
@Test public void testGetTrafficSources() throws Exception { AnalyticsReportsDataProvider analyticsReportsDataProvider = new AnalyticsReportsDataProvider( _getHttp( Collections.singletonMap( "/traffic-sources", JSONUtil.putAll( JSONUtil.put( "name", "organic" ).put( "trafficAmount", 3849L ).put( "trafficShare", 94.25D ), JSONUtil.put( "name", "paid" ).put( "trafficAmount", 235L ).put( "trafficShare", 5.75D ) ).toString()))); List<TrafficSource> trafficSources = analyticsReportsDataProvider.getTrafficSources( RandomTestUtil.randomLong(), RandomTestUtil.randomString()); Assert.assertEquals( trafficSources.toString(), 2, trafficSources.size()); Assert.assertEquals( new TrafficSource(null, "organic", 3849L, 94.25D), trafficSources.get(0)); Assert.assertEquals( new TrafficSource(null, "paid", 235L, 5.75D), trafficSources.get(1)); }
@Test(expected = PortalException.class) public void testGetTrafficSourcesWithAsahFaroBackendError() throws Exception { AnalyticsReportsDataProvider analyticsReportsDataProvider = new AnalyticsReportsDataProvider(_getHttp(new IOException())); analyticsReportsDataProvider.getTrafficSources( RandomTestUtil.randomLong(), RandomTestUtil.randomString()); }
@Test public void testGetTrafficSourcesWithCountrySearchKeywords() throws Exception { AnalyticsReportsDataProvider analyticsReportsDataProvider = new AnalyticsReportsDataProvider( _getHttp( Collections.singletonMap( "/traffic-sources", JSONUtil.putAll( JSONUtil.put( "countryKeywords", JSONUtil.putAll( JSONUtil.put( "countryCode", "us" ).put( "countryName", "United States" ).put( "keywords", JSONUtil.putAll( JSONUtil.put( "keyword", "liferay" ).put( "position", 1 ).put( "searchVolume", 3600 ).put( "traffic", 2880L ), JSONUtil.put( "keyword", "liferay portal" ).put( "position", 1 ).put( "searchVolume", 390 ).put( "traffic", 312L )) )) ).put( "name", "organic" ).put( "trafficAmount", 3192L ).put( "trafficShare", 93.93D ), JSONUtil.put( "countryKeywords", JSONUtil.putAll( JSONUtil.put( "countryCode", "us" ).put( "countryName", "United States" ).put( "keywords", JSONUtil.putAll( JSONUtil.put( "keyword", "dxp enterprises" ).put( "position", 1 ).put( "searchVolume", 4400 ).put( "traffic", 206L )) )) ).put( "name", "paid" ).put( "trafficAmount", 206L ).put( "trafficShare", 6.07D ) ).toString()))); List<TrafficSource> trafficSources = analyticsReportsDataProvider.getTrafficSources( RandomTestUtil.randomLong(), RandomTestUtil.randomString()); Assert.assertEquals( trafficSources.toString(), 2, trafficSources.size()); Assert.assertEquals( new TrafficSource( Collections.singletonList( new CountrySearchKeywords( "us", Arrays.asList( new SearchKeyword("liferay", 1, 3600, 2880L), new SearchKeyword( "liferay portal", 1, 390, 312L)))), "organic", 3192L, 93.93D), trafficSources.get(0)); Assert.assertEquals( new TrafficSource( Collections.singletonList( new CountrySearchKeywords( "us", Collections.singletonList( new SearchKeyword( "dxp enterprises", 1, 4400, 206L)))), "paid", 206L, 6.07D), trafficSources.get(1)); }