GA4 Analytics Spatie or similar
Hey folks, so I seem to be stuck on a particular issue and not certain as how to proceed,
When running the below I get the following errors
{
"message": "User does not have sufficient permissions for this profile.",
"domain": "global",
"reason": "insufficientPermissions"
}
Code:
$analyticsConfig = config('analytics');
$client = AnalyticsClientFactory::createForConfig($analyticsConfig);
$analytic = new Analytics($client, $analyticsConfig['property_id']);
$analyticsData = $analytic->fetchVisitorsAndPageViews(SpatiePeriod::days(7));
Everything is passed in as needed from what I can gather and I also tested my credentials using a different package https://github.com/akki-io/laravel-google-analytics which returns data. So I might be having some type of issue with Spatie or it might be general, not sure as yet.
Secondly, with GA4 what would be the best course of action to process something as per:
GoogleAnalyticsService::getSessions($ga_client_id, $this->start_date, $this->end_date);
I store the ga_client_id in my DB for a particular set of "leads"/visitors and this would be what I run for them
foreach($sessions as $session)
{
$platform = $session->getPlatform();
$category = $session->getDeviceCategory();
$datasource = $session->getDataSource();
$act = $session->getActivities();
$ga_session_id = $session->getSessionId();
$ga_session_date = $session->getSessionDate();
$tblSession = $this->lead->gaSessions()->where('ga_session_id', $ga_session_id)->first();
if ($tblSession == null)
{
$tblSession = $this->lead->gaSessions()->create([
'platform' => $platform,
'category' => $category,
'data_source' => $datasource,
'ga_session_id' => $ga_session_id,
'ga_session_date' => $ga_session_date
]);
}
foreach($act as $activity)
{
$tblSession->activities()->create([
'lead_guid' => $this->lead->uuid,
'session_uuid' => $tblSession->uuid,
'activity_time' => $activity->getActivityTime(),
'device' => $category,
'activity' => $activity->getActivityType(),
'channel' => $activity->getChannelGrouping(),
'source' => $activity->getSource(),
'medium' => $activity->getMedium(),
'campaign' => $activity->getCampaign(),
'keyword' => $activity->getKeyword()
]);
}
$tblSession->resolve();
}
// When we're done with all the above, see if we can attribute the lead
$this->lead->refreshAttribution();
Then simply on the lead I just update my table as:
public function refreshAttribution()
{
if (!$this->hasGoogleAnalytics()) return;
$successfullyAttributed = false;
$oldestPaidSearchSession = $this->gaSessions()
->orderBy('ga_session_date', 'ASC')
->where('channel', 'Paid Search')
->first();
if ($oldestPaidSearchSession != null) {
$this->attrib_uuid = $oldestPaidSearchSession->uuid;
$this->attrib_channel = $oldestPaidSearchSession->channel;
$this->attrib_source = $oldestPaidSearchSession->source;
$this->attrib_medium = $oldestPaidSearchSession->medium;
$this->attrib_campaign = $oldestPaidSearchSession->campaign;
$this->attrib_keyword = $oldestPaidSearchSession->keyword;
$this->attrib_to = "Oldest Paid Search Session";
$this->attrib_at = now('Africa/Johannesburg');
$this->save();
$successfullyAttributed = true;
}
if (!$successfullyAttributed) {
$oldestNonPaidSession = $this->gaSessions()
->orderBy('ga_session_date', 'ASC')
->first();
if ($oldestNonPaidSession != null) {
$this->attrib_uuid = $oldestNonPaidSession->uuid;
$this->attrib_channel = $oldestNonPaidSession->channel;
$this->attrib_source = $oldestNonPaidSession->source;
$this->attrib_medium = $oldestNonPaidSession->medium;
$this->attrib_campaign = $oldestNonPaidSession->campaign;
$this->attrib_keyword = $oldestNonPaidSession->keyword;
$this->attrib_to = "Oldest Non-Paid Session";
$this->attrib_at = now('Africa/Johannesburg');
$this->save();
$successfullyAttributed = true;
}
}
if (!$successfullyAttributed) {
$this->attrib_channel = "UNATTRIBUTED";
$this->attrib_source = "UNATTRIBUTED";
$this->attrib_medium = "UNATTRIBUTED";
$this->attrib_campaign = "UNATTRIBUTED";
$this->attrib_keyword = "UNATTRIBUTED";
$this->attrib_to = "Cannot attribute lead";
$this->attrib_at = now('Africa/Johannesburg');
$this->save();
}
}
Would this still be possible with GA4 as I have not yet found a way of doing so and any suggestion or help would be appreciated.
Please or to participate in this conversation.