'flutter app - trending posts by using google analytics data api

In my app, users create posts and I'd like to show trending posts by the number of views, comments, etc in a specific date range. To do that I thought I can create a custom event as below:

await FirebaseAnalytics.instance.logEvent(
    name: "trending_contents",
    parameters: {
      "content_type": EnumToString.convertToString(type),
      "content_id": contentModel.externalId,
      "action_type": "post",
      "point": 3,
    },
  );

I wonder if it is possible to use Google Analytics Data API to get trending posts by a specific date range? Or is there any better way to get trending posts instead of google analytics data API?



Solution 1:[1]

I finally found a solution on how to use Google Analytics Data API to manage trending content. If anyone is looking for a solution for a similar need, here is what I've done so far:

  1. I send a custom event in specific situations such as when the user views the content etc. as below. If you use parameters' names according to predefined dimensions & metrics (see API Dimensions & Metrics), it will be easy to prepare a custom report (at least it was for me...). Later, I use contentType and contentId as dimensions and eventValue as a metric in the custom report.

     await FirebaseAnalytics.instance.logEvent(
          name: "trending_contents",
          parameters: {
            "content_type": EnumToString.convertToString(event.type),
            "content_id": contentId,
            "action_type": "view",
            "value": 1,
          },
        );
    
  2. Lastly, I created a scheduled cloud function that runs every 6 hours and populates firebase collection according to custom report results. This report gives contentIds in a specific date range ordered by the sum of values that I sent in a custom event

P.S. you need to create a service account in Google Cloud Console, then generate JSON credentials for it and add the file to your project (see credentialsJsonPath variable below). Then you need to add its email address to google analytics 'Property Access Management' section to access analytics data. To see Google Analytics Data API samples, you can check their GitHub repo

    const { BetaAnalyticsDataClient } = require('@google-analytics/data');
    exports.scheduledTrendingFunction = functions.pubsub.schedule('0 */6 * * *').onRun((context) => {
    const propertyId = process.env.GA_PROPERTY_ID;
    const credentialsJsonPath = process.env.GA_CRENDENTIALS_PATH;
    const analyticsDataClient = new BetaAnalyticsDataClient({
        keyFilename: credentialsJsonPath,
    });

    async function runReport(filterType) {
        // [START analyticsdata_json_credentials_run_report]
        const [response] = await analyticsDataClient.runReport({
            property: `properties/${propertyId}`,
            dateRanges: [
                {
                    startDate: '3daysAgo',
                    endDate: 'today',
                },
            ],
            dimensions: [
                {
                    name: 'contentType',
                },
                {
                    name: 'contentId'
                }
            ],
            metrics: [
                {
                    name: 'eventValue'
                },
            ],
            dimensionFilter: {
                andGroup: {
                    expressions: [
                        {
                            filter: {
                                fieldName: "eventName",
                                inListFilter: {
                                    values: ["trending_contents"]
                                }
                            }
                        },
                        {
                            filter: {
                                fieldName: "contentType",
                                inListFilter: {
                                    values: [filterType]
                                }
                            }
                        }
                    ]
                }
            },
            offset: 0,
            limit: 20,
            orderBys: [
                {
                    desc: true,
                    metric: {
                        metricName: "eventValue"
                    }
                }
            ]
        });
        // [END analyticsdata_json_credentials_run_report]

        const batch = admin.firestore().batch();

        // BATCH: delete
        const trendRef = admin.firestore().collection('trends').doc(filterType);
        batch.delete(trendRef);

        const subTrendRef = admin.firestore().collection('trends').doc(filterType).collection('trendContents');
        
        // console.log(response);
        response.rows.forEach((row, index) => {
            // BATCH: add each contentId to trend
            const contentId = row['dimensionValues']['1']['value'];
            batch.set(subTrendRef.doc(contentId), {priority: index + 1});
        });

        // Commit the batch
        await batch.commit();
    }

    runReport("book");

    return null;
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anilcngz