How to send category name from Wordpress to Google Analytics Content Groups (universal tracking code) - universal-analytics

I want to enable content grouping in Google Analytics by submitting the category of my posts from within the tracking code.
A solution for GA's async version of the tracking code would look like this:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
<?php
if (is_single()){
echo “_gaq.push(['_setPageGroup', 1, '".get_the_author()."']);\n”;
$category = get_the_category();
if ($category && !empty($category[0]->cat_name)){
echo “_gaq.push([‘_setPageGroup’, 2, ‘”.$category[0]->cat_name.”‘]);\n”;
}
}
?>
_gaq.push(['_trackPageview']);
How can I set up the same functionality using GA's universal tracking code?
Thanks!
Best regards,
Alex

Classic GA syntax:
_gaq.push(['_setPageGroup', '<Index Number>', '<Group Name>']);
UA syntax:
ga('set', 'contentGroup<Index Number>', '<Group Name>');
So in your case it should/would/could be this:
<?php
if (is_single()){
echo "ga('set', contentGroup1, '".get_the_author()."');\n";
$category = get_the_category();
if ($category && !empty($category[0]->cat_name)){
echo "ga('set', contentGroup2, '".$category[0]->cat_name."');\n”;
}
}
?>
More information can be found here: https://support.google.com/analytics/answer/2853546?hl=en

Related

Joomla module not working

I made a module that display how many days ago a article was published
it looks like this.
{source}
<?php
$jinput = JFactory::getDocument()->input;
$option = $jinput->get('option');
$view = $jinput->get('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& $jinput->get("content");
$article->load($article_id);
$date = new JDate($article->get("publish_up"));
$currentTime = new JDate('now');
$interval = $date->diff($currentTime);
if($interval->d == 0) {
echo 'dzisiaj' . "<br>";
}
else if( $interval->d == 1) {
echo 'wczoraj' . "<br>";
}
else if( $interval->d > 1) {
echo $interval->format('%a dni temu') . "<br>";
}
}
?>
{/source}
And it works on my local joomla but when use it on custom template it doesnt work. I'm using Joomla 3.4.8.
The issue is you're trying to access the input values using Document Factory that's wrong you have to use
$jinput = JFactory::getApplication()->input;
Document Factory is used for other purpose like adding , styles or Js to the pages etc. read more about input here.
Hope it make sense.

Facebook API images not downloading

First post here,
i am developing a app to fetch and download facebook images from user's account.
I have a basic code till now as follows..
Need help as to why only the 1 file keeps downloading alone...in the zip i create
it is the only image without the ?oh= concatenation, all others with the oh parameter dont get
downloaded...
<?php
set_time_limit(0);
$url='https://graph.facebook.com/me/photos/uploaded?access_token=CAACEdEose0cBAEGw8R4QOGfN3bfX8qtuZCCy8FX6g5Sil8CKaYlp9x8lZAiNFEGJXhh9MYOWdZAJR1x2M627rc5I3n3AauVe8nJtYBsehjUZAljMLkQMMxP03VVXknZBa81Nk1S23z7SnyTkIlgt2oZC1euoFNCTuGesAap1uQUGZAX7LnILlBwyj3V7IVo9QQCNv66Lv8UcA86wYn9jlJE';
$result = file_get_contents($url);
$a=json_decode($result, true);
$files = array();
echo '<div class="col-lg-12">';
$num= count($a['data']);
echo "<table class='table table-hover table-responsive'>";
for($o=0;$o<$num;$o++)
{
$files_to_zip[$o]=$a['data'][$o]['source'];
if($a['data'][$o]['source']!="")
{
echo "<tr><td><img src='".$a['data'][$o]['source']."' id=".$o." width='200' height='200' /></td></tr>";
array_push($files,$a['data'][$o]['source']);
//echo $files_to_zip[$o]."<br/><br/><br/><br/>";
}
}
echo '</table></div><br/>';
?>

How to filter New WP Query by Custom Field Value?

I am creating new pages for each of my categories in wordpress. The post editor has a custom field that allows the selection of a sector type, this gets applied to the post on update. The custom field key is: sector, for custom field meta value options lets use SectorA, SectorB and SectorC. I am using a custom post type called projects.
I followed the advice at this link http://weblogtoolscollection.com/archives/2008/04/13/how-to-only-retrieve-posts-with-custom-fields/
How can I change the query line in the code below so that it filters the loop by a Sector name, lets use SectorA. I'll then reuse the code on each template page changing the value to SectorB and SectorC on the other pages.
I think this needs changing somehow:
$customPosts->query('showposts=5&sector=sectorA&post_type=projects' );
Currently it echos the sector value and description value successfully but is showing all the posts. So my attempt to limit it to sectorA using sector=sectorA doesn't seem to work?
This code is in functions.php:
function get_custom_field_posts_join($join) {
global $wpdb, $customFields;
return $join . " JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key in ($customFields)) ";
}
function get_custom_field_posts_group($group) {
global $wpdb;
$group .= " $wpdb->posts.ID ";
return $group;
}
And this code is on the Template Page:
<?php /* Begin Custom Field Posts */ ?>
<h2>Custom Posts</h2>
<ul>
<?php
global $customFields;
$customFields = "'sector', 'description'";
$customPosts = new WP_Query();
add_filter('posts_join', 'get_custom_field_posts_join');
add_filter('posts_groupby', 'get_custom_field_posts_group');
$customPosts->query('showposts=5&sector=sectorA&post_type=projects' );//Uses same parameters as query_posts
remove_filter('posts_join', 'get_custom_field_posts_join');
remove_filter('posts_groupby', 'get_custom_field_posts_group');
while ($customPosts->have_posts()) : $customPosts->the_post();
$sector = get_post_custom_values("sector");
$description= get_post_custom_values("description");?>
<li><?php echo $sector[0]; ?></li>
<li><?php echo $description[0]; ?></li><br />
<?php endwhile; ?>
</ul>
<?php /* End Custom Field Posts */ ?>
Thanks for your help
May be this what you want
function get_custom_field_posts_join($join) {
global $wpdb, $customSector;
return $join . " JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key = 'sector' and postmeta.value = '$customSector') ";
}
and modification of page
$customSector='sectorA';//<--- insert this
add_filter('posts_join', 'get_custom_field_posts_join');
Try using this code.
<?php
$sector = get_post_meta($post->ID, "sector", false);
if ($sector[0]=="") { ?>
<!-- If there are no custom fields, show nothing -->
<?php } else { ?>
<div class="sector">
<h3>Title</h3>
<?php foreach($sector as $sector) {
echo '<blockquote><p>'.$sector.'</p></blockquote>';
} ?>
</div>
<?php } ?>

View Helper in Kohana

I am trying to figure out a way to do the following:
I want to make an action which will be loaded through ajax and also its the internal part of the page when page is reloaded.
I know this in ZEND framework by using View Helper, But don't know how to do in Kohana
I am new to Kohana.
EDIT:
Example of what I am trying to do http://www.espncricinfo.com/west-indies-v-india-2011/engine/current/match/489228.html?CMP=chrome
In above webpage when the whole web page is loaded the live score board is loaded with it. But when u click on "Refresh scoreboard" button only the live score board is replaced through ajax.
I want to create an action say action_scoreboard which will be used to bring scoreboard data. And action_index to load the whole page, but while in the view of action_index i need to call action_scoreboard.
Thanks
Not sure if this is the best way to do this, but this is how I like to handle the situation.
public function action_index($raw = 0) {
$records = Jelly::select('scores')->execute();
if ($raw == 0) {
$view = new View('purdy');
$view->records = $records;
$this->template->content = $view;
} else {
$this->auto_render = FALSE;
$this->request->headers['Content-Type'] = 'text/xml';
$view = new View('raw');
$view->records = $records;
$this->response->body($view->render());
}
}
### THE PURDY VIEW ###
<table>
<?
foreach ($records as $record) {
echo '<tr>';
echo '<td>'.$record->name.'</td>';
echo '<td>'.$record->value.'</td>';
echo '</tr>';
}
?>
</table>
### THE RAW VIEW ###
<?xml version="1.0" encoding="utf-8"?>
<scores>
<?
foreach ($records as $record) {
echo '<score>';
echo '<name>'.$record->name.'</name>';
echo '<value>'.$record->value.'</value>';
echo '</score>';
}
?>
</scores>
I used Kopjax - Pjax jQuery ajax module. Its code is available on gitgub

How can I fetch information about the app/song/video etc. from iTunes Store?

I need to get an info about the app/song/video by item id from iTunes Store.
I've found this
But it doesn't work with apps.
Is there any public API?
UPD: I can get info using this link
, but this is not a structured data it's just a markup for iTunes to display stuff. I can't rely on that - it can be changed anytime and is hard to parse because it has no consistent structure...
Apple now seems to offer a friendlier search service returning JSON. NB: the documentation does stipulate that the API is for use in connection with promoting the search results (i.e. it's designed for affiliate links).
Example, fetching info about an app if you know its Apple ID:
http://itunes.apple.com/lookup?id=[appleID]
General keyword search
http://itunes.apple.com/search?term=[query]
As far as I know (and I've done a lot of looking), there isn't a public API.
You're right that the HTML isn't semantically structured, so parsing it won't be very robust. But I think it's your only option. Here are a few links which might help :-
A Python script which parses reviews.
An Ars Technica article: Linking to the stars: hacking iTunes to solicit reviews.
An Inside iPhone article: Scraping AppStore Reviews.
There is a public API into iTunes called "iTunes Store Web Service Search API" that returns quite a bit of information. Some of it is documented here but that documentation is incomplete.
You can use the API to get information about everything for sale in iTunes Store and App Store including urls for the artwork, links directly into iTunes, all the apps by a developer, and so on. It's very robust and I'd love to find updated documentation.
I'm currently writing an article at the iPhone Dev FAQ to show how a few things are done and extend the available documentation.
That link you have there is JSON! You've got the solution right here. You just need JSON.framework
I wrote this script for myself. It's not optimized or future-proof, but it's working for me in the meantime...
<?php
ini_set('display_errors', false);
if(isset($_GET['appID']) && isset($_GET['format']))
{
$appID = (int)stripslashes($_GET['appID']);
$format = stripslashes($_GET['format']);
$url = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=$appID&mt=8";
$useragent = "iTunes/4.2 (Macintosh; U; PPC Mac OS X 10.2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$result = curl_exec($ch);
curl_close($ch);
$temp = str_replace("½","",strip_tags(substr($result,
strpos($result,"Average rating for the current version:"),
strpos($result,"Rate this application:")-strpos($result,"Average rating for the current version:"))));
$temp1 = explode("ratings",$temp);
if(strpos($temp1[2], "Average rating for all versions:"))
$temp1[2] = substr($temp1[2],0,stripos($temp1[2],"Average rating for all versions:"));
$temp1[2] = preg_replace('/\s\s+/', ' ', $temp1[2]);
$temp2 = explode(" ",$temp1[2]);
$ratings[0] = $temp2[1];
$ratings[1] = $temp2[2];
$ratings[2] = $temp2[3];
$ratings[3] = $temp2[4];
$ratings[4] = $temp2[5];
if($format == "prettyPrint")
printRatings($ratings);
else if($format == "XML");
getXML($ratings);
}
else
{
echo "Enter the app id and format (http://iblackjackbuddy.com/getAppRatings.php?appID=###&format=###";
}
function printRatings($ratings)
{
echo "Five stars: " . $ratings[0];
echo "<br>Four stars: " . $ratings[1];
echo "<br>Three stars: " . $ratings[2];
echo "<br>Two stars: " . $ratings[3];
echo "<br>One star: " . $ratings[4];
echo "<hr>Total ratings: " . getTotalRatings($ratings);
echo "<br>Average rating: " . getAverageRating($ratings);
}
function getTotalRatings($ratings)
{
$temp = 1;
for($i=0; $i < count($ratings); ++$i)
$temp+=$ratings[$i];
return $temp;
}
function getAverageRating($ratings)
{
$totalRatings = getTotalRatings($ratings);
return round(5*($ratings[0]/$totalRatings)
+ 4*($ratings[1]/$totalRatings)
+ 3*($ratings[2]/$totalRatings)
+ 2*($ratings[3]/$totalRatings)
+ 1*($ratings[4]/$totalRatings),2);
}
function getXML($ratings)
{
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<Rating>';
echo '<FiveStars>'.$ratings[0].'</FiveStars>';
echo '<FourStars>'.$ratings[1].'</FourStars>';
echo '<ThreeStars>'.$ratings[2].'</ThreeStars>';
echo '<TwoStars>'.$ratings[3].'</TwoStars>';
echo '<OneStar>'.$ratings[4].'</OneStar>';
echo '<TotalRatings>'.getTotalRatings($ratings).'</TotalRatings>';
echo '<AverageRating>'.getAverageRating($ratings).'</AverageRating>';
echo '</Rating>';
}
?>