Reputation: 13
I'm new to C# and I am creating a console application which connects to Google Analytics using the API. I have got it so it writes the data grabbed in the console and exports it to a CSV however I have specified dates here:
static void Main(string[] args) {
Authenticate();
**getVisits("2013-01-01", "2015-01-28");**
PercentInc();
}
I need it to start from when I first started tracking the website without entering manually. Reason: The idea of this tool is too loop through a database table consisting of the GA ids and then write the data into a CSV document but for future it will be put in a database.
Here is the getVisits function
static void getVisits(string fromDate, string toDate) {
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
"ga:" + websiteCode,
fromDate,
toDate,
"ga:users");
request.Dimensions = "ga:year,ga:month,ga:day";
var data = request.Execute();
foreach (var row in data.Rows) {
DateTime visitDate = new DateTime(int.Parse(row[0]), int.Parse(row[1]), int.Parse(row[2]));
int numVisits = int.Parse(row[3]);
if (numVisits == 0) {
}
string filePath = "exports/" + clientGAID + ".csv";
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Completed [" + visitDate.ToString("dd-MM-yyyy") + "] Client: " + clientGAID + " {" + numVisits + "}");
if (!File.Exists(filePath)) {
File.Create(filePath).Close();
}
string delimiter = ",";
string[][] output = new string[][]{
new string[]{visitDate.ToString("dd-MM-yyyy") + "," + numVisits}
};
int length = output.GetLength(0);
StringBuilder sb = new StringBuilder();
for (int index = 0; index < length; index++)
sb.AppendLine(string.Join(delimiter, output[index]));
File.AppendAllText(filePath, sb.ToString());
}
}
So to put briefly here is what I need to do:
Notes:
Thanks in advance.
Upvotes: 1
Views: 142
Reputation: 116908
This is a very good question.
Option 1:
Make a request
This will return the first date that there is date for the account then you can use that. Query Explore example
Option 2:
Request the info from the Mangagment API. The management api returns the created date of the account. Account.list
Note: remember if you grab all this data at once changes are your going to end up with sampled data. Make sure your requests are small.
Upvotes: 1