0

I have this line of codes at my Controller to get data from google analytics

$analyticsData = Analytics::performQuery(Period::days(30),
    'ga:',
    [
        'metrics' => 'ga:users,ga:newUsers,ga:sessions,ga:sessionsPerUser,ga:pageviews,ga:pageviewsPerSession,ga:avgSessionDuration,ga:bounceRate',
        'start-date' => '2018-08-01',
        'end-date' => '2018-09-02',
        'dimensions' => 'ga:pageTitle',
    ]);

When I dd($analyticsData); This is what I got.

analytics

This is my migration query.

    Schema::create('analytics', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('site_id');
        $table->string('site_url');
        $table->integer('report_id');
        $table->integer('analytics_view_id'); 
        $table->date('reportGenerated');
        $table->integer('gaUsers');
        $table->integer('gaNewUsers');
        $table->integer('gaSessions');
        $table->integer('gaSessionsPerUser');
        $table->integer('gaPageviews');
        $table->integer('gaPageviewsPerSession');
        $table->integer('gaAvgSession');
        $table->integer('gaBounceRate');

    });

Now my problem is how can I able to stores those array data to my database? Thank you.

2 Answers 2

1

It would be something like:

$model = Analytics::create([
   'gaUsers' => $analyticsData['ga:users'],
   'gaNewUsers' => $analyticsData['ga:newUsers'],
   // ... and so on...
]);

Just in case make sure you have those fields in $fillable attribute of your Analytics model.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks now I will make my model as well.
1

Try:

$analytic = new Analytics();

$analytic->gaUsers = $analyticsData['ga:users'];
$analytic->gaNewUsers = $analyticsData['ga:newUsers'];
...
$analytic->save();

8 Comments

Sorry, it won't work. $analyticsData is array, and object properties cannot have colon
It would be fine now.
@MarcinNabiałek I had some data that needs to pull from other tables. and save it to my analytics table as well is it okay if I use this line of commands? Like for example I need to pull my site_url from sites table.
@GihanLakshan how do you think?
simply use $analytic->site_url = $url;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.