php - How to access Google Analytics API from a serverside background service? -


I want to get the user to drag Google Analytics data into my database without any background cron jobs every time.

I know as I had asked before. Using OAuth will not work, as it requires user interaction. According to Oath, an access token can also be used to access page statistics per session basis. Although I am looking for a continuous way to implement it in a background service.

How can I use Google Analytics without the user's authentication or end time for an access token?

You can do the same as you consider the account in question. Make sure that you read the service account email address in the Google Analytics account to read at the account level and it will be able to read your data.

  & lt ;? Php session_start (); Need_once 'Google / Client.php'; Need_once 'Google / Service / Analytics.php'; / *************************************************************************************************** ******************************************************************************************** The Google Developer Console After the 3 values ​​in the settings for the application that you created, a beef key key file should be placed in a location that is not accessible from the Web. To access your GA account outside the web route, you must add an email address as a user at the ACCOUNT level in GA Admin. **************************************************************************************************** **************************************************************************************************** Client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com'; $ Email_adress = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com'; $ Key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12'; $ Client = new Google_Client (); $ Customer & gt; SetApplicationName ("Client_Library_Examples"); $ Key = file_get_contents ($ key_file_location); // Extra scope with a comma $ scopes = "https://www.googleapis.com/auth/analytics.readonly"; $ Cred = New Google_Auth_AssertionCredentials ($ email address, array ($ scopes), $ key); $ Customer & gt; SetAssertionCredentials ($ cred); If ($ client- & gt; getAuth () - & gt; isxTokenAxide ()) {$ client-> GetAuth () - & gt; RefreshTokenWeight Assurance ($ cred); } $ Service = new Google_Service_Analytics ($ customer); $ Accounts = $ service-> Management_CountrySundices- & gt; List management authority (); // Calling start date $ date = new date time (date ("Y-M-D")); $ Date-> Sub (new date difference ('P10 D')); // Add dimension $ params = array ('dimension' = & gt; 'ga: userType'); // Data Request $ data = $ Service-> Data_ga- & gt; Receive ("ga: 78110423", $ date-> format ('Ym-d'), date ("Ymd"), "ga: user, Ga: session", $ params); ? & Gt; & Lt; Html & gt; & Lt ;? Php echo $ date- & gt; Format ('Y-M-D') "-" .Det ("Wi-M-D") "\ N" ;? & Gt; & Lt; Table & gt; & Lt; TR & gt; & Lt ;? Php // printing column headers foreach ($ data-> getColumnHeaders ($$ header)) {print " gt; $ Header ['name']. "& Lt; / td>"; }? & Gt; & Lt; / TR & gt; & Lt ;? Php / line $ [1] $ each line printing $ (Data-> GetRows) as [$] {Print "  $ Line [0]. "& Lt; / td> & lt; td & gt; $ Line [1] "& Lt; / td> gt; & lt; td & gt; $ Line [2] "& Lt; / td> gt; & lt; / tr & gt;"; } // Printing the total number of rows? & Gt; & Lt; Tr & gt; & Lt; Td colspan = "2" & gt; Return lines & lt ;? Php Print $ data- & gt; GetTotalResults ();; & Gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; & Lt; / Html & gt; & Lt ;? Php? & Gt;  

The code is rebutted from the tutorial

If this is not your account then you can use the normal oth 2 method to authenticate you once Be able to access the data you can use. Using the code from your previous question.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -