Multi-Author Google Analytics (Combining PHP Variable from DB to JS)

I am trying to let authors on my site retrieve their own Google analytics data by setting up a channel in Analytics and submitting it via their profile. I ahve been trying plenty of ways to do this, but here is what I have so far.

window.google_analytics_uacct = “UA-xxxxxxx-xx”;

Read More
<script type="text/javascript">//<![CDATA[

var _gaq = _gaq || [];
_gaq.push(['_setAccount','UA-xxxxxxx-xx']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setAccount', 'UA-xxxxxxx-xx']);
_gaq.push(['_trackPageview']);
(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
//]]></script>

I was trying to incorporate the following PHP around the second account data is pushed to.

    <?php if ( get_the_author_meta( 'analytics' ) ) { ?>
        <?php the_author_meta( 'analytics' ); ?>
    <?php } ?>

I was using the if statement around the second set of

_gaq.push(['_setAccount', 'UA-xxxxxxx-xx']);
_gaq.push(['_trackPageview']);

With the view of only displaying this set if the site author has submitted their Analytics ID.

Unfortunately just getting the value from the database in itself is causing me a lot of problems! Any help would be appreciated 🙂

Kind Regards

Oli

Related posts

Leave a Reply

1 comment

  1. -Try this one. Author meta ‘analytics’ must be like UA-1234567-12

    <script type="text/javascript">//<![CDATA[
    
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount','UA-xxxxxxx-xx']);
    
    <?php if($ganal = get_the_author_meta('analytics')): ?>
    _gaq.push(['_setAccount', '<?php echo $ganal; ?>']);
    <?php endif; ?>
    
    _gaq.push(['_trackPageview']);
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
    //]]></script>