Load data from Google Analytics in a Chart.js chart












0















My question is: How do I load data from Google Analytics in a Chart.js chart?



I am writing a program with the Laravel framework (version 5.6) and use the Google Analytics API (not the Google Analytics Embed API)(version 3).



I get the data correctly presented in an array but I want to show it in a graph.



Subquestion: Do I need to replace the data in datasets with a connection to the Google Analytics API?



A simple chart from Chart.js:



<canvas id="userChart"></canvas>
<script>
var ctx = document.getElementById('userChart').getContext('2d');
var chart = new Chart(ctx, {

// The type of chart we want to create
type: 'line',

// The data for our dataset
data: {
labels: ["dag 1", "dag 8", "dag 15", "dag 22", "dag 30"],
datasets: [{
label: "users",
//backgroundColor: '#2f5ec4',
borderColor: '#2f5ec4',
data: [138, 163, 115, 124, 49],
}]
},
// Configuration options go here
options: {}
});



Update:
Example of how I get the Analytics data:



Route::get('/datatest', function()
{
$analyticsData =
Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));
dd($analyticsData);
});


Example of the DD array:



DD array










share|improve this question

























  • how and where is this array of data obtained from ? You don't show us what you are asking kinda weird

    – GaimZz
    May 31 '18 at 13:47











  • @GaimZz Thanks for your comment! I updated my question with how I ask the data through a GET method in a route. This is how I test parts before I implement it further in the application.

    – sophievda
    May 31 '18 at 14:27
















0















My question is: How do I load data from Google Analytics in a Chart.js chart?



I am writing a program with the Laravel framework (version 5.6) and use the Google Analytics API (not the Google Analytics Embed API)(version 3).



I get the data correctly presented in an array but I want to show it in a graph.



Subquestion: Do I need to replace the data in datasets with a connection to the Google Analytics API?



A simple chart from Chart.js:



<canvas id="userChart"></canvas>
<script>
var ctx = document.getElementById('userChart').getContext('2d');
var chart = new Chart(ctx, {

// The type of chart we want to create
type: 'line',

// The data for our dataset
data: {
labels: ["dag 1", "dag 8", "dag 15", "dag 22", "dag 30"],
datasets: [{
label: "users",
//backgroundColor: '#2f5ec4',
borderColor: '#2f5ec4',
data: [138, 163, 115, 124, 49],
}]
},
// Configuration options go here
options: {}
});



Update:
Example of how I get the Analytics data:



Route::get('/datatest', function()
{
$analyticsData =
Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));
dd($analyticsData);
});


Example of the DD array:



DD array










share|improve this question

























  • how and where is this array of data obtained from ? You don't show us what you are asking kinda weird

    – GaimZz
    May 31 '18 at 13:47











  • @GaimZz Thanks for your comment! I updated my question with how I ask the data through a GET method in a route. This is how I test parts before I implement it further in the application.

    – sophievda
    May 31 '18 at 14:27














0












0








0








My question is: How do I load data from Google Analytics in a Chart.js chart?



I am writing a program with the Laravel framework (version 5.6) and use the Google Analytics API (not the Google Analytics Embed API)(version 3).



I get the data correctly presented in an array but I want to show it in a graph.



Subquestion: Do I need to replace the data in datasets with a connection to the Google Analytics API?



A simple chart from Chart.js:



<canvas id="userChart"></canvas>
<script>
var ctx = document.getElementById('userChart').getContext('2d');
var chart = new Chart(ctx, {

// The type of chart we want to create
type: 'line',

// The data for our dataset
data: {
labels: ["dag 1", "dag 8", "dag 15", "dag 22", "dag 30"],
datasets: [{
label: "users",
//backgroundColor: '#2f5ec4',
borderColor: '#2f5ec4',
data: [138, 163, 115, 124, 49],
}]
},
// Configuration options go here
options: {}
});



Update:
Example of how I get the Analytics data:



Route::get('/datatest', function()
{
$analyticsData =
Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));
dd($analyticsData);
});


Example of the DD array:



DD array










share|improve this question
















My question is: How do I load data from Google Analytics in a Chart.js chart?



I am writing a program with the Laravel framework (version 5.6) and use the Google Analytics API (not the Google Analytics Embed API)(version 3).



I get the data correctly presented in an array but I want to show it in a graph.



Subquestion: Do I need to replace the data in datasets with a connection to the Google Analytics API?



A simple chart from Chart.js:



<canvas id="userChart"></canvas>
<script>
var ctx = document.getElementById('userChart').getContext('2d');
var chart = new Chart(ctx, {

// The type of chart we want to create
type: 'line',

// The data for our dataset
data: {
labels: ["dag 1", "dag 8", "dag 15", "dag 22", "dag 30"],
datasets: [{
label: "users",
//backgroundColor: '#2f5ec4',
borderColor: '#2f5ec4',
data: [138, 163, 115, 124, 49],
}]
},
// Configuration options go here
options: {}
});



Update:
Example of how I get the Analytics data:



Route::get('/datatest', function()
{
$analyticsData =
Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));
dd($analyticsData);
});


Example of the DD array:



DD array







php laravel google-analytics chart.js google-analytics-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 31 '18 at 14:31







sophievda

















asked May 31 '18 at 12:13









sophievdasophievda

5912




5912













  • how and where is this array of data obtained from ? You don't show us what you are asking kinda weird

    – GaimZz
    May 31 '18 at 13:47











  • @GaimZz Thanks for your comment! I updated my question with how I ask the data through a GET method in a route. This is how I test parts before I implement it further in the application.

    – sophievda
    May 31 '18 at 14:27



















  • how and where is this array of data obtained from ? You don't show us what you are asking kinda weird

    – GaimZz
    May 31 '18 at 13:47











  • @GaimZz Thanks for your comment! I updated my question with how I ask the data through a GET method in a route. This is how I test parts before I implement it further in the application.

    – sophievda
    May 31 '18 at 14:27

















how and where is this array of data obtained from ? You don't show us what you are asking kinda weird

– GaimZz
May 31 '18 at 13:47





how and where is this array of data obtained from ? You don't show us what you are asking kinda weird

– GaimZz
May 31 '18 at 13:47













@GaimZz Thanks for your comment! I updated my question with how I ask the data through a GET method in a route. This is how I test parts before I implement it further in the application.

– sophievda
May 31 '18 at 14:27





@GaimZz Thanks for your comment! I updated my question with how I ask the data through a GET method in a route. This is how I test parts before I implement it further in the application.

– sophievda
May 31 '18 at 14:27












1 Answer
1






active

oldest

votes


















2














It might be too late but for anyone who is in need, try the following. For your case you have your $analyticsData collection, which is a good start. Chartjs uses lists to map and compare data.
So for example we want to see a chart of date vs visitors



//transfer all of this code out of the routes to your controller

$analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));

//send these two variables to your view

$dates = $analyticsData->pluck('date');
$visitors = $analyticsData->pluck('visitors');


In your script, the labels should receive $dates in a list as follows;



labels: {!! json_encode($dates) !!},


And data in datasets receive $visitors as a list;



data: {!! json_encode($visitors) !!}


See this article on my website for more in-depth explanations.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50624055%2fload-data-from-google-analytics-in-a-chart-js-chart%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    It might be too late but for anyone who is in need, try the following. For your case you have your $analyticsData collection, which is a good start. Chartjs uses lists to map and compare data.
    So for example we want to see a chart of date vs visitors



    //transfer all of this code out of the routes to your controller

    $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));

    //send these two variables to your view

    $dates = $analyticsData->pluck('date');
    $visitors = $analyticsData->pluck('visitors');


    In your script, the labels should receive $dates in a list as follows;



    labels: {!! json_encode($dates) !!},


    And data in datasets receive $visitors as a list;



    data: {!! json_encode($visitors) !!}


    See this article on my website for more in-depth explanations.






    share|improve this answer






























      2














      It might be too late but for anyone who is in need, try the following. For your case you have your $analyticsData collection, which is a good start. Chartjs uses lists to map and compare data.
      So for example we want to see a chart of date vs visitors



      //transfer all of this code out of the routes to your controller

      $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));

      //send these two variables to your view

      $dates = $analyticsData->pluck('date');
      $visitors = $analyticsData->pluck('visitors');


      In your script, the labels should receive $dates in a list as follows;



      labels: {!! json_encode($dates) !!},


      And data in datasets receive $visitors as a list;



      data: {!! json_encode($visitors) !!}


      See this article on my website for more in-depth explanations.






      share|improve this answer




























        2












        2








        2







        It might be too late but for anyone who is in need, try the following. For your case you have your $analyticsData collection, which is a good start. Chartjs uses lists to map and compare data.
        So for example we want to see a chart of date vs visitors



        //transfer all of this code out of the routes to your controller

        $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));

        //send these two variables to your view

        $dates = $analyticsData->pluck('date');
        $visitors = $analyticsData->pluck('visitors');


        In your script, the labels should receive $dates in a list as follows;



        labels: {!! json_encode($dates) !!},


        And data in datasets receive $visitors as a list;



        data: {!! json_encode($visitors) !!}


        See this article on my website for more in-depth explanations.






        share|improve this answer















        It might be too late but for anyone who is in need, try the following. For your case you have your $analyticsData collection, which is a good start. Chartjs uses lists to map and compare data.
        So for example we want to see a chart of date vs visitors



        //transfer all of this code out of the routes to your controller

        $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));

        //send these two variables to your view

        $dates = $analyticsData->pluck('date');
        $visitors = $analyticsData->pluck('visitors');


        In your script, the labels should receive $dates in a list as follows;



        labels: {!! json_encode($dates) !!},


        And data in datasets receive $visitors as a list;



        data: {!! json_encode($visitors) !!}


        See this article on my website for more in-depth explanations.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 17:59

























        answered Nov 14 '18 at 1:31









        bryceandybryceandy

        615




        615






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50624055%2fload-data-from-google-analytics-in-a-chart-js-chart%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly