How to i check how many has not read a specific article with GA4 API in php

35 Views Asked by At

I need to sort out how many users, in an old set of data (not that old, but too old for me to be able to set up content groups), that has read at least 2 articles out of a set of about 50. When I'm working with that I find that both the andFilter and the notFilter are acting wierd.

$test->setDimensionFilter((new FilterExpression())->setFilter(new Filter
            ([
                'field_name' => 'pagePath',
                'string_filter' => new Filter\StringFilter([
                'value' => $paths[0],
                'match_type' => Filter\StringFilter\MatchType::CONTAINS,
            ]),
        ])));


        $test2->setDimensionFilter((new FilterExpression())->setNotExpression(new FilterExpression([
            'filter' => new Filter([
                'field_name' => 'pagePath',
                'string_filter' => new Filter\StringFilter([
                'value' => $paths[0],
                'match_type' => Filter\StringFilter\MatchType::CONTAINS,
            ]),
        ]),
])));

This should sum up to the amount of total users, but it really doesn't. (the metric is set to ActiveUsers and but no dimension i set)

I'm guessing that the reason to why it does this is because test2 is filtering everyone that has not visited the article in any of their sessions?

Now about AND:

$test3->setDimensionFilter((new FilterExpression())->setAndGroup(new FilterExpressionList([ 
    'expressions'=> [
        new FilterExpression([
            'filter' => new Filter([
                'field_name' => 'pagePath',
                'string_filter' => new Filter\StringFilter([
                    'value' => $paths[0],
                    'match_type' => Filter\StringFilter\MatchType::CONTAINS,
                    ]),
                ]),
            ]),
        new FilterExpression([
            'filter' => new Filter([
                'field_name' => 'pagePath',
                'string_filter' => new Filter\StringFilter([
                    'value' => $paths[1],
                    'match_type' => Filter\StringFilter\MatchType::CONTAINS,
                    ]),
                ]),
            ]),
        ]
    ])
    )
);

This returns no rows, even though Exploration mode in GA4 gives 71 users in the same session 91 over multiple sessions.The code works as it's supposed if i use "setOrGroup", instead of setAndGroup.

So, does anyone have any idea what could be wrong with the filtering for NOT and AND? Extra credit to anyone that has an idea on how i can get hold of the amount of users that has read two of more articles out of a set of articles, without predefining anyting (like audiences or content groups that has to be set in advance)!

I have tried to set them up both by that json structure above, and by defining every step on the way, like this:

        $filterExpression = new FilterExpression();
        $filterExpression->setFilter($filter1);
        //set up for second
        $filter2->setFieldName("pagePath");
        $stringFilter2 = new StringFilter();
        $stringFilter2->setValue($paths[1]);
        $filter2->setStringFilter($stringFilter2);

        $filterExpression1 = new FilterExpression();
        $filterExpression1->setFilter($filter2);

I've tried with different paths, other than that, I can't really think of much I can try? I can't find any reason to why AND does not work, and the api is not very descriptive on how it assesses the NOT-logic https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/FilterExpression

0

There are 0 best solutions below