You can use **Archive** instances to get data that was archived for one or more sites,
for one or more periods and one optional segment.
If archive data is not found, this class will initiate the archiving process. 1
**Archive** instances must be created using the {@link build()} factory method;
they cannot be constructed.
You can search for metrics (such as nb_visits) using the {@link getNumeric()} and
{@link getDataTableFromNumeric()} methods. You can search for
reports using the {@link getBlob()}, {@link getDataTable()} and {@link getDataTableExpanded()} methods.
If you're creating an API that returns report data, you may want to use the
{@link createDataTableFromArchive()} helper function.
### Learn more
Learn more about _archiving_ here.
### Limitations
- You cannot get data for multiple range periods in a single query.
- You cannot get data for periods of different types in a single query.
### Examples
**_Querying metrics for an API method_**
one site and one period
$archive = Archive::build($idSite = 1, $period = 'week', $date = '2013-03-08');
return $archive->getDataTableFromNumeric(array('nb_visits', 'nb_actions'));
all sites and multiple dates
$archive = Archive::build($idSite = 'all', $period = 'month', $date = '2013-01-02,2013-03-08');
return $archive->getDataTableFromNumeric(array('nb_visits', 'nb_actions'));
**_Querying and using metrics immediately_**
one site and one period
$archive = Archive::build($idSite = 1, $period = 'week', $date = '2013-03-08');
$data = $archive->getNumeric(array('nb_visits', 'nb_actions'));
$visits = $data['nb_visits'];
$actions = $data['nb_actions'];
... do something w/ metric data ...
multiple sites and multiple dates
$archive = Archive::build($idSite = '1,2,3', $period = 'month', $date = '2013-01-02,2013-03-08');
$data = $archive->getNumeric('nb_visits');
$janSite1Visits = $data['1']['2013-01-01,2013-01-31']['nb_visits'];
$febSite1Visits = $data['1']['2013-02-01,2013-02-28']['nb_visits'];
... etc.
**_Querying for reports_**
$archive = Archive::build($idSite = 1, $period = 'week', $date = '2013-03-08');
$dataTable = $archive->getDataTable('MyPlugin_MyReport');
... manipulate $dataTable ...
return $dataTable;
**_Querying a report for an API method_**
public function getMyReport($idSite, $period, $date, $segment = false, $expanded = false)
{
$dataTable = Archive::createDataTableFromArchive('MyPlugin_MyReport', $idSite, $period, $date, $segment, $expanded);
return $dataTable;
}
**_Querying data for multiple range periods_**
get data for first range
$archive = Archive::build($idSite = 1, $period = 'range', $date = '2013-03-08,2013-03-12');
$dataTable = $archive->getDataTableFromNumeric(array('nb_visits', 'nb_actions'));
get data for second range
$archive = Archive::build($idSite = 1, $period = 'range', $date = '2013-03-15,2013-03-20');
$dataTable = $archive->getDataTableFromNumeric(array('nb_visits', 'nb_actions'));
[1]: The archiving process will not be launched if browser archiving is disabled
and the current request came from a browser.