简体   繁体   中英

How to create Custom Dimension & get Unique PageView with GA4?

We're not sure if this is the right place for us to obtain the answers of such. We've contacted the Firebase Support Team but did not get the answers that we want except a few links to the online documentation that we've mostly been through before. After further clarifying our requests, we've not been receiving any response from them for 5 days; therefore we might as well try our luck here.

1. How to create a Custom Dimension in GA4?

As we understand, GA4 are all events now, including the Hit in Universal Analytics (UA) , but how can we map from our UA custom dimensions to the GA4 model as shown below?

在此处输入图像描述

When we tried to create the AccCode custom dimension in GA4, we have no idea what to enter under the Event parameter dropdown list as it also cannot be dropped down whatsoever:

在此处输入图像描述

May I know how can we proceed from here and what should we enter for the Event parameter value?

2. How to get Unique PageView (UPV) in Firebase GA4 API?

In UA or GA v3, this is how we get our Page View and Unique PageView :

return jwt.authorize()
   .then((response) => {
      return google.analytics('v3').data.ga.get({
         'auth': jwt,
         'ids': 'ga:' + gaConfig.ViewerID,
         'start-date': validatedDateRange.strStartDate,
         'end-date': validatedDateRange.strEndDate,
         'metrics': 'ga:pageviews,ga:uniquepageviews',
         "dimensions": gaConfig.AccCodeDimension,
         'filters': ${gaConfig.PageUrlDimension}!@googleusercontent.com;${gaConfig.PageUrlDimension}!@blogspot.com${!accCode ? "" : ";" + gaConfig.AccCodeDimension + "==" + accCode}`,
         'sort': `${gaConfig.AccCodeDimension},-ga:pageviews,-ga:uniquepageviews`
      }).then((gaQuery) => {
         // Do something here
      });

Below is the sample code that we found from the Firebase GA4 documentation :

import firebase from "firebase/app";
import "firebase/analytics";

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Initialize Analytics and get a reference to the service
const analytics = firebase.analytics();

analytics.logEvent('select_content', {
  content_type: 'image',
  content_id: 'P12453',
  items: [{ name: 'Kittens' }]
});

But the above sample code seems to be far from giving us an idea on how to achieve the same result as did in GA v3. It's more like logging of event data, potentially for our custom dimensions as what we did in the UA's tracking code. Examples for data pulling don't seem to be available in the documentation here . We need an equivalent example on how we can achieve the same result with Firebase GA4 API so that we can quickly migrate over to GA4 before July 2023 .

We hope that someone here can help us to resolve the above two issues as quickly as possible because they involve changing the core engine of our app, which requires vast amount of development time and testing while the clock is ticking. Thank you so much in advance!

After so much of the studies and R&D, we realized that for what we're trying to achieve has nothing to do Firebase at all -- we can purely focus on GA and its latest API, which is still on Beta while some are on Alpha . But for the custom dimension creation, below is the answer:

Creating Custom Dimensions in GA4

As per the question described, the custom dimension creation process can be very confusing, especially to the UA users due to the change of data model and concepts. But what you need to know is that, you need to finalize your event parameters before mapping them over to the custom dimensions on GA console because the event parameter cannot be edited once the custom dimension is created on GA console:

在此处输入图像描述

So what you need to do is to extend your existing UA tracking code as shown below before creating your custom dimensions on GA console:

gtag('event','page_view', { // the page_view event is for web
    "acc_code": "{{{AccCode}}}",   // acc_code is your event parameter
    "acc_name": "{{{AccName}}}",   // This is another event parameter
    "viewer_id": "{{{ViewerID}}}",
    "viewer_title": "{{{ViewerTitle}}}",
    "viewer_url": "{{{gaUrl}}}"
});
gtag('config', 'G-XXXXXXXX'); // This is your GA4 MEASUREMENT_ID

Data Query in GA4

For data query in GA4 that is equivalent to the given example in the question, please refer to the GA4 Data API here .

What about the Unique Pageview metric?

According to the GA documentation here , looks like Unique Pageview is no longer available in GA4:

在此处输入图像描述

I'm not sure if this is still subject to change, but you may need to write your own code, perhaps using sessionStorage or session cookies to track your own unique pageviews per user session for every page viewed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM