We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27716
    • 126 Posts
    Hi Guys,

    Im wondering how I might populate a template variable with data from Google Analytics.

    At the moment i’m manually taking data from google analytics to populate a video views TV.
    As my video library grows this is becoming laborious.

    I would like to take data from GA event tracking an automatically populate a TV with it.

    Any guidance to the general approach would be helpful. (Snippets / API etc?)

    This may be something i create a bounty for.

    Stuart


      • 33968
      • 863 Posts
      I’ve not done this before but it seemed interesting.

      Had a quick look at the GA API. You can build and test your query on Google’s data Explorer. Click the Metrics dropdown and scroll down near the bottom to find Event Tracking. Here’s the reference for Event Tracking.

      Without looking further into it yet, I don’t think a TV would be the way to go but you would probably need some kind of snippet/chunk combination depending on what data you want to display and how you want to display it.
        • 27716
        • 126 Posts
        Tanks so much for the info lucas.

        Will look at that tool from Google and see what I can produce then maybe try get it all wrapped up by putting a bounty up.

        Stuart
          • 33968
          • 863 Posts
          Found this one as well (php interface) which will be much more useful for integrating with MODx.
            • 6437
            • 157 Posts
            Is this for front end display? Hence the TV?

            I’d be very interested in seeing this written in to a custom manager tab. I’ve noticed that addons seem to be orientated towards creating custom modules rather than integrating with the default resource view where the date is often more appropriate.

            Page level data from GA per resource would be quite easy to achieve with the API me thinks, the limitation is in working out how to get Modx to display it.

            Cheers,

            DM
              • 18913
              • 654 Posts
              Interesting question. Here’s what I came up with using the above gapi link.

              1) Create a snippet something like this one. I’ve left in commented-out code so this is rough. The idea is to generate a TV with radio buttons, allowing you to select whether total pageviews or visits are displayed. It assumes that the gapi code has been put in assets/snippets/gapi.
              <?php
              require 'actual/filepath/to/assets/snippets/gapi/gapi.class.php';
              
              $output = '';
              define('ga_email','your ga email addy');
              define('ga_password','your ga email password');
              define('ga_profile_id','your ga profile id');
              
              $ga = new gapi(ga_email,ga_password);
              
              if ($ga)
              {
                $ga->requestAccountData();
              /*
                foreach($ga->getResults() as $result)
                {
                  $output .= $result . ' (' . $result->getProfileId() . ")<br />";
                }
              */
                $ga->requestReportData(ga_profile_id,array('browser','browserVersion'),array('pageviews','visits'));
              /*
                foreach($ga->getResults() as $result)
                {
                  $output .= '<strong>'.$result.'</strong><br />';
                  $output .= 'Pageviews: ' . $result->getPageviews() . ' ';
                  $output .= 'Visits: ' . $result->getVisits() . '<br />';
                }
              
                $output .= '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>';
              */
                $output .= 'Total pageviews==' . $ga->getPageviews() . '||Total visits==' . $ga->getVisits();
              }
              else
              {
                $output = 'NA==0';
              }
              return $output;
              ?>
              


              2) Create a TV and assign it to the appropriate template:
              Type : Radio Options
              Default input value : @EVAL return $modx->runSnippet(’the_above_snippet_name’);

              3) In a page using that template, select your option and in the resource’s content put something like
              My total page views are [*this_template_variable_name*]


              This was just quick and dirty. I haven’t played around with what other data is made available.

              Hope this helps,
              Matt

              EDIT : This was tested in Evo 1.0.5. I tried running it in Revo 2.0.7 but it seemed to get hung up and I don’t know why. FWIW ...