开发者

Google Analytics tracking events within php

开发者 https://www.devze.com 2023-03-14 12:14 出处:网络
I want to track events via google analytics for items loading on the page. Basically if a banner i开发者_开发知识库mage shows on a page I want to track the impression via an event using google analyti

I want to track events via google analytics for items loading on the page. Basically if a banner i开发者_开发知识库mage shows on a page I want to track the impression via an event using google analytics. So for example:

pageTracker._trackEvent('BannerImages','Impression','Banner Ad Name Here');

Where would I put this? Here is the php code:

$query = $dbconn->query("SELECT * FROM banner_images LIMIT 5");

foreach($query AS $banner_result)
{

echo '<div>' .$banner_result->location .'</div>';

}

Would I echo out the pageTracker code after each value is echoed out for $banner_result->location like so?

echo '<div>' .$banner_result->location .'<div>' ."\n" .'<script>pageTracker._trackEvent('BannerImages','Impression','Banner Ad Name Here');</script>';

The google tracking code is in the footer.

EDIT

Looks like I have to add it after the tracking code initializes.


I would collect all pageTracker items in an array

foreach($query AS $banner_result){
    echo '<div>' .$banner_result->location .'</div>';
    $googleAnalytics[] = "pageTracker._trackEvent('BannerImages','Impression','".$banner_result->yourBannerName."');";
}

And add them after you included the async Google Analytics code:

echo '<script>'.implode("\n",$googleAnalytics).'</script>';
0

精彩评论

暂无评论...
验证码 换一张
取 消