index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<script src="https://apis.google.com/js/api.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script> function authenticate() { return gapi.auth2.getAuthInstance() .signIn({scope: "https://www.googleapis.com/auth/webmasters https://www.googleapis.com/auth/webmasters.readonly"}) .then(function() { console.log("Sign-in successful"); }, function(err) { console.error("Error signing in", err); }); } function loadClient() { gapi.client.setApiKey("###yourapikey###"); return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/searchconsole/v1/rest") .then(function() { console.log("GAPI client loaded for API"); }, function(err) { console.error("Error loading GAPI client for API", err); }); } // Make sure the client is loaded and sign-in is complete before calling this method. function execute() { return gapi.client.webmasters.searchanalytics.query({ "siteUrl": "sc-domain:codingshiksha.com", "resource": { "startDate": "2021-11-11", "endDate": "2022-01-11", "dimensionFilterGroups": [ { "filters": [ { "dimension": "PAGE", "operator": "EQUALS", "expression": "https://codingshiksha.com/tutorials/join-500-mobile-hardware-accessories-or-repairing-whatsapp-group-invite-links-for-android-ios-mobile-app-developers-programmers/" }, ] } ] } }) .then(function(response) { // Handle the results here (response.result has the parsed body). console.log(response) $("#data").html(` <tr> <td>${response.result.rows[0].impressions}</td> <td>${response.result.rows[0].clicks}</td> <td>${response.result.rows[0].position}</td> </tr> `) console.log(response.result.rows[0].clicks); }, function(err) { console.error("Execute error", err); }); } gapi.load("client:auth2", function() { gapi.auth2.init({client_id: "##yourclientid##"}); }); </script> <button onclick="authenticate().then(loadClient)">authorize and load</button> <button onclick="execute()">execute</button> <div id="result"> <table class="table table-striped"> <thead> <tr> <th>Impressions</th> <th>Clicks</th> <th>Position</th> </tr> </thead> <tbody id="data"> </tbody> </table> </div> |
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<script src="https://apis.google.com/js/api.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script> function authenticate() { return gapi.auth2.getAuthInstance() .signIn({scope: "https://www.googleapis.com/auth/webmasters https://www.googleapis.com/auth/webmasters.readonly"}) .then(function() { console.log("Sign-in successful"); }, function(err) { console.error("Error signing in", err); }); } function loadClient() { gapi.client.setApiKey("####yourapikey####"); return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/searchconsole/v1/rest") .then(function() { console.log("GAPI client loaded for API"); }, function(err) { console.error("Error loading GAPI client for API", err); }); } // Make sure the client is loaded and sign-in is complete before calling this method. function execute() { return gapi.client.webmasters.searchanalytics.query({ "siteUrl": "sc-domain:codingshiksha.com", "resource": { "searchType": "WEB", "dimensions": [ "PAGE" ], "startDate": "2018-05-05", "endDate": "2022-01-18", "rowLimit": 500 } }) .then(function(response) { // Handle the results here (response.result has the parsed body). console.log("Response", response); var html = '' response.result.rows.forEach(page => { $("#data").append(` <tr> <td>${page.keys[0]}</td> <td>${page.impressions}</td> <td>${page.clicks}</td> <td>${page.position}</td> </tr> `) }); }, function(err) { console.error("Execute error", err); }); } gapi.load("client:auth2", function() { gapi.auth2.init({client_id: "####yourclientid####"}); }); </script> <button onclick="authenticate().then(loadClient)">authorize and load</button> <button onclick="execute()">execute</button> <div id="result"> <table class="table table-striped"> <thead> <tr> <th>Page</th> <th>Impressions</th> <th>Clicks</th> <th>Position</th> </tr> </thead> <tbody id="data"> </tbody> </table> </div> |
Just replace the client id
and api key
inside the above code from the google cloud console