How to get Google analytics data in our database and after first time authorize data all data store and automatically update daily without authorization.
once I authorize in Google account and after authorize and store data in our data base and again login in site so in this time how to update all Google analytics data in our database please help me.
I wait for your reply.
You can use the core reporting api v4. Whenever you perform a request you get a response with a pageToken. The final response is the one that returns a null pageToken. This is the key criterion to get all the data you want! Keep in mind that each request requests at most 10000 rows of data! You need some credentials to get authorized for accessing google analytics. There are two ways: The first one is using a json file and the second one using the .p12 file. In bothe cases you need a view Id. For more details you can have a look at core reporting api v4 documentation! Each time you perform a request one more authentication is triggered! It is inevitable! GA api is extremely fast though!
if u want to access google analyitics data first u must use offline so we can get refresh token this referesh token store in your database if u want to again u r data without authorization u can use this refresh token and generate access token so now each and every time u can get updated google analytics data by using offline access .........
Related
We have the following scenario:
Website A has GTAG installed and tracks every page visit, sending a internalUserId along in the dataLayer (when the user is logged).
Website B is a CRM where we list all users, and it uses the same database as Website A.
In the CRM we need to see what pages each of our users have visited.
Proposed solution:
Ideal solution: call Analytics API passing internalUserId and returning a list of pages the user visited.
Alternative 1: make Website A call an internal API that registers a visit, along with any other metadata. Unfortunately the dev team behind Website A wants to avoid this approach as it will send yet another request on every page, thus slowing the page down.
So the suggestion was to leverage analytics to gather this information.
Alternative 2: somehow instruct Tag manager to call an external API every time a visit occurs, and send along any meta data. I don't actually know if Tag manager offers this service or not.
The alternative 1 & 2 will mean that we will store the visit stats, while the ideal solution will use Analytics stats.
So the question is:
Does Analytics API offer the ability to query for a specific user identified by a custom property, internalUserId, that we set using dataLayer, and return that user's stats (visits) ?
Thank you very much
I don't believe you can query Google Analytics API using your internalUserId unless you set it as the user ID in GA, which may lead to limitations. You'd be better off merging the data by collecting the _ga cookie along with you internalUserID values. Look up all user activity by _ga and then merge to your CRM data with internalUserID.
I don't believe alternative 2 is possible.
Alternative 1 would require you store the data yourself and a client side call, but you'd avoid ongoing GA API usage. The calls from client side could be minimal. Use a Cloudflare worker to fetch and process only the data you need, that could be an interesting solution.
I am trying to create a view count feature for a webpage. To do this, I created a new property and generated a tracking id from Google Analytics.
Is there a way I can use the page vists data from there and display it on the same page that I am tracking?
You can, but since GA APIs are authenticated, you will need to handle that authentication one way or another.
You have basically 3 options:
User doing OAuth: by default you would need to implement the OAuth flow like the Query Explorer is doing, which is obviously not adequate if the counter is meant to be public, as visitors won't have the appropriate GA permissions to get data from that property
Pre generated access token: you can generate the access token manually, and use that token to make the requests:
https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference#auth-options
serverAuth.access_token Type: string
If you already have a valid access token, you can pass it to the
authorize method directly and the user will not be prompted to
authorize. For details on how to retrieve an access token, see the
OAuth 2.0 documentation.
Middleware: you can use a tool to pull data from the GA API and then query data from that tool. For instance you can:
Use the GA add-on to pull your data to a Google Sheet
Import that data into another Google Sheet with IMPORTRANGE
Publish the Google Sheet from 2 so it's publicly readable via API (for instance as CSV). You will be provided with a URL that looks like https://docs.google.com/spreadsheets/d/e/{some-uid}/pub?gid=0&single=true&output=csv
Fetch data from that URL from the website (eg with jQuery)
Note: you probably don't have to do step 2 if you're careful, and can publish as CSV your sheet from steo 1 directly: since it's a CSV export, people cannot get to the original spreadsheet so no security risk. But just to be on the safe side (eg you could publish the whole spreadsheet by mistake), I would do step 2.
As for which middleware you want to use, there's probably an infinite number of options :)
There is an API in place and available to me (v4 compatible), but for various reasons it's not working correctly and there is not much time to fix it (too many other fires). So the idea was floated that that to bypass most of the issues with API here it might be a quick-fix to send whole query to GA.
It needs to be automated and results stored in our DB (Webservice on MS Stack), else we would do it manually all the way, which is out of the question at this time.
Hence the question:
IS there a way to submit a query that is exactly the product of a Query Explorer via API or any other way and retrieve it's results?
The API Query URI returned by the Query Explorer
https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A78110423&start-date=30daysAgo&end-date=yesterday&metrics=ga%3Ametric1
Is for the Google Analytics v3 api. To use this in your own code you must simply attach a valid access token to the end of the request.
https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A78110423&start-date=30daysAgo&end-date=yesterday&metrics=ga%3Ametric1&access_token=XXXX
This is not going to be using the Google Analytics v4 API that is a different call entirely.
Access tokens can be retrieved from either service account authentication or Oauth2 authentication and must be valid for the view you are requesting data for.
I'm currently creating a tool to help me create google analytics daily reports easily. To do this, I need to access google analytics data at specific hours and days (for example, each day at 9am and 9pm), server side (PHP, Laravel).
The problem is that when I use the OAuth 2 authentication method, I need to re-login each time I want to access data (i need to redirect the user, and wait for redirection to get the code, and use it to get a token). I can use the "service account" method, but it implies to use a .p12 certificate, which is not sustainable in production (i can't ask my users to upload their certificates files).
Do you know a way, or have you an idea, to access analytics data without having to relogging (ideally with oauth...) or using service account method (with a certificate file) ?
Thank you
How to generate Google Analytics tracking code programmatic-ally?
I am new to Google Analytics API, can someone please help
It is NOT possible to create new Google Analytics accounts or profiles programmatically using the existing APIs.
There are two APIs available, the Data Export API and the Management API. However, they are bot read-only.
With the Google Analytics Data Export API, you can develop client applications to request data from an existing Analytics profile for an authorized user, and refine the results of the request using query parameters. Currently, the Data Export API supports read-only access to your Google Analytics data.
Currently, the Management API supports read-only access to five components of the Google Analytics Management system: Account data,Profile data, Web property data, Goal data, Advanced segments
Seems like it's finally possible in 2021 with alpha api.
https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/create#authorization-scopes
It's not currently possible to create Google Analytics accounts automatically at the moment but generating the tracking code is possible.
The only real variation between the tracking code generated each time you create a profile for a new domain is the web property ID. This is in the format UA-XXXXXXX-X. Each X is a number - the first 7 numbers are the account number and the number after the last dash relates to each web property set up for the account.
You can use the accounts feed of the API to get a list of accounts and their profiles. If an account only has one profile then it's possible to use the web property ID of it to automatically generate the tracking code. However, if there's multiple web properties then you would need a way to decide which one to use (present the user with a selection list etc).