গুগল অ্যানালিটিক্স এপিআই কুইকস্টার্ট (original) (raw)

আপনি এই কুইকস্টার্টের জন্য ডেটা API বা অ্যাডমিন API ব্যবহার করতে পারেন

আপনি একটি ব্যবহারকারী অ্যাকাউন্ট বা পরিষেবা অ্যাকাউন্ট দিয়ে প্রমাণীকরণ করতে পারেন:

একটি অ্যাকাউন্টের ধরন চয়ন করুন:

এই কুইকস্টার্টে, আপনি একটি runReport অনুরোধ তৈরি করে পাঠান।

এখানে পদক্ষেপগুলির একটি সারাংশ রয়েছে:

  1. সরঞ্জাম এবং অ্যাক্সেস সেট আপ করুন।
  2. API সক্ষম করুন।
  3. একটি SDK ইনস্টল করুন।
  4. একটি API কল করুন।

আপনি শুরু করার আগে

  1. একটি পরিষেবা অ্যাকাউন্ট তৈরি করুন
  2. একটি Google ক্লাউড VM উদাহরণ তৈরি করুন
  3. gcloud CLI ইনস্টল করুন এবং আরম্ভ করুন
  4. আপনার পরিষেবা অ্যাকাউন্টকে প্রয়োজনীয় সুযোগ দিতে এবং এটিকে আপনার VM উদাহরণের সাথে লিঙ্ক করতে, নিম্নলিখিতগুলি চালান:
gcloud compute instances stop VM-INSTANCE-NAME  
gcloud compute instances set-service-account VM-INSTANCE-NAME \  
    --service-account SERVICE-ACCOUNT-EMAIL  \  
    --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/analytics.readonly"  
  1. Google Analytics UI- তে, একটি Google Analytics সম্পত্তিতে পরিষেবা অ্যাকাউন্টের অ্যাক্সেস মঞ্জুর করুন৷

ডেটা API সক্ষম করুন

একটি Google ক্লাউড প্রকল্প নির্বাচন বা তৈরি করতে এবং API সক্ষম করতে, Google Analytics ডেটা API v1 সক্ষম করুন ক্লিক করুন।

Google Analytics ডেটা API v1 সক্ষম করুন৷

একটি SDK ইনস্টল করুন

আপনার প্রোগ্রামিং ভাষার জন্য SDK ইনস্টল করুন।

একটি API কল করুন

আপনার সেটআপ যাচাই করতে এবং একটি API কল করতে, নিম্নলিখিত নমুনাটি চালান।

এই নমুনা runReport পদ্ধতি কল. প্রতিক্রিয়া আপনার সম্পত্তির জন্য সক্রিয় ব্যবহারকারীদের তালিকাভুক্ত করে।

সমস্ত Analytics API কোড নমুনা ইনস্টল করতে, আমাদের GitHub দেখুন।

জাভা

import com.google.analytics.data.v1beta.BetaAnalyticsDataClient; import com.google.analytics.data.v1beta.DateRange; import com.google.analytics.data.v1beta.Dimension; import com.google.analytics.data.v1beta.Metric; import com.google.analytics.data.v1beta.Row; import com.google.analytics.data.v1beta.RunReportRequest; import com.google.analytics.data.v1beta.RunReportResponse;

/**

public static void main(String... args) throws Exception { /** * TODO(developer): Replace this variable with your Google Analytics 4 property ID before * running the sample. */ String propertyId = "YOUR-GA4-PROPERTY-ID"; sampleRunReport(propertyId); }

// This is an example snippet that calls the Google Analytics Data API and runs a simple report // on the provided GA4 property id. static void sampleRunReport(String propertyId) throws Exception { // Using a default constructor instructs the client to use the credentials // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) {

  RunReportRequest request =
      RunReportRequest.newBuilder()
          .setProperty("properties/" + propertyId)
          .addDimensions(Dimension.newBuilder().setName("city"))
          .addMetrics(Metric.newBuilder().setName("activeUsers"))
          .addDateRanges(DateRange.newBuilder().setStartDate("2020-03-31").setEndDate("today"))
          .build();

  // Make the request.
  RunReportResponse response = analyticsData.runReport(request);

  System.out.println("Report result:");
  // Iterate through every row of the API response.
  for (Row row : response.getRowsList()) {
    System.out.printf(
        "%s, %s%n", row.getDimensionValues(0).getValue(), row.getMetricValues(0).getValue());
  }
}

} }

পিএইচপি

require 'vendor/autoload.php';

use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient; use Google\Analytics\Data\V1beta\DateRange; use Google\Analytics\Data\V1beta\Dimension; use Google\Analytics\Data\V1beta\Metric; use Google\Analytics\Data\V1beta\RunReportRequest;

/**

// Using a default constructor instructs the client to use the credentials // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. $client = new BetaAnalyticsDataClient();

// Make an API call. $request = (new RunReportRequest()) ->setProperty('properties/' . $property_id) ->setDateRanges([ new DateRange([ 'start_date' => '2020-03-31', 'end_date' => 'today', ]), ]) ->setDimensions([new Dimension([ 'name' => 'city', ]), ]) ->setMetrics([new Metric([ 'name' => 'activeUsers', ]) ]); response=response = response=client->runReport($request);

// Print results of an API call. print 'Report result: ' . PHP_EOL;

foreach ($response->getRows() as $row) { print $row->getDimensionValues()[0]->getValue() . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL; }

পাইথন

from google.analytics.data_v1beta import BetaAnalyticsDataClient from google.analytics.data_v1beta.types import ( DateRange, Dimension, Metric, RunReportRequest, )

def sample_run_report(property_id="YOUR-GA4-PROPERTY-ID"): """Runs a simple report on a Google Analytics 4 property.""" # TODO(developer): Uncomment this variable and replace with your # Google Analytics 4 property ID before running the sample. # property_id = "YOUR-GA4-PROPERTY-ID"

# Using a default constructor instructs the client to use the credentials
# specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
client = BetaAnalyticsDataClient()

request = RunReportRequest(
    property=f"properties/{property_id}",
    dimensions=[Dimension(name="city")],
    metrics=[Metric(name="activeUsers")],
    date_ranges=[DateRange(start_date="2020-03-31", end_date="today")],
)
response = client.run_report(request)

print("Report result:")
for row in response.rows:
    print(row.dimension_values[0].value, row.metric_values[0].value)

Node.js

/**

// Imports the Google Analytics Data API client library. const {BetaAnalyticsDataClient} = require('@google-analytics/data');

// Using a default constructor instructs the client to use the credentials // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. const analyticsDataClient = new BetaAnalyticsDataClient();

// Runs a simple report. async function runReport() { const [response] = await analyticsDataClient.runReport({ property: properties/${propertyId}, dateRanges: [ { startDate: '2020-03-31', endDate: 'today', }, ], dimensions: [ { name: 'city', }, ], metrics: [ { name: 'activeUsers', }, ], });

console.log('Report result:');
response.rows.forEach((row) => {
  console.log(row.dimensionValues[0], row.metricValues[0]);
});

}

runReport();

.নেট

using Google.Analytics.Data.V1Beta; using System;

namespace AnalyticsSamples { class QuickStart { static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID") { /** * TODO(developer): Uncomment this variable and replace with your * Google Analytics 4 property ID before running the sample. */ // propertyId = "YOUR-GA4-PROPERTY-ID";

        // Using a default constructor instructs the client to use the credentials
        // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
        BetaAnalyticsDataClient client = BetaAnalyticsDataClient.Create();

        // Initialize request argument(s)
        RunReportRequest request = new RunReportRequest
        {
            Property = "properties/" + propertyId,
            Dimensions = { new Dimension{ Name="city"}, },
            Metrics = { new Metric{ Name="activeUsers"}, },
            DateRanges = { new DateRange{ StartDate="2020-03-31", EndDate="today"}, },
        };

        // Make the request
        RunReportResponse response = client.RunReport(request);

        Console.WriteLine("Report result:");
        foreach(Row row in response.Rows)
        {
            Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
        }
    }
    static int Main(string[] args)
    {
        if (args.Length > 0) {
            SampleRunReport(args[0]);
        } else {
            SampleRunReport();
        }
        return 0;
    }
}

}

বিশ্রাম

এই অনুরোধ পাঠাতে, কমান্ড লাইন থেকে curl কমান্ড চালান বা আপনার অ্যাপ্লিকেশনে REST কল অন্তর্ভুক্ত করুন।

curl -X POST
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)"
-H "x-goog-user-project: ${PROJECT_ID}"
-H "Content-Type: application/json"
-d ' { "dateRanges": [ { "startDate": "2025-01-01", "endDate": "2025-02-01" } ], "dimensions": [ { "name": "country" } ], "metrics": [ { "name": "activeUsers" } ] }' https://analyticsdata.googleapis.com/v1beta/properties/${PROPERTY_ID}:runReport

এখানে JSON-এ একটি নমুনা প্রতিক্রিয়া রয়েছে:

{
  "dimensionHeaders": [
    {
      "name": "country"
    }
  ],
  "metricHeaders": [
    {
      "name": "activeUsers",
      "type": "TYPE_INTEGER"
    }
  ],
  "rows": [
    {
      "dimensionValues": [
        {
          "value": "United States"
        }
      ],
      "metricValues": [
        {
          "value": "3242"
        }
      ]
    },
    {
      "dimensionValues": [
        {
          "value": "(not set)"
        }
      ],
      "metricValues": [
        {
          "value": "3015"
        }
      ]
    },
    {
      "dimensionValues": [
        {
          "value": "India"
        }
      ],
      "metricValues": [
        {
          "value": "805"
        }
      ]
    }
  ],
  "rowCount": 3,
  "metadata": {
    "currencyCode": "USD",
    "timeZone": "America/Los_Angeles"
  },
  "kind": "analyticsData#runReport"
}