Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

mobile version adding of google adsense codes

hi, im not well versed on smarty functions, and i've tried putting the google ad value for mobile in the template but it gets bonk. the sample code is here:

    <?php

    $GLOBALS['google']['client']='ca-mb-pub-xxxxxxxxxxxx';
    $GLOBALS['google']['https']=read_global('HTTPS');
    $GLOBALS['google']['ip']=read_global('REMOTE_ADDR');
    $GLOBALS['google']['markup']='xhtml';
    $GLOBALS['google']['output']='xhtml';
    $GLOBALS['google']['ref']=read_global('HTTP_REFERER');
    $GLOBALS['google']['slotname']='xxxxxxxxxxxxxx';
    $GLOBALS['google']['url']=read_global('HTTP_HOST') . read_global('REQUEST_URI');
    $GLOBALS['google']['useragent']=read_global('HTTP_USER_AGENT');
    $google_dt = time();
    google_set_screen_res();
    google_set_muid();
    google_set_via_and_accept();
    function read_global($var) {
      return isset($_SERVER[$var]) ? $_SERVER[$var]: '';
    }

    function google_append_url(&$url, $param, $value) {
      $url .= '&' . $param . '=' . urlencode($value);
    }

    function google_append_globals(&$url, $param) {
      google_append_url($url, $param, $GLOBALS['google'][$param]);
    }

    function google_append_color(&$url, $param) {
      global $google_dt;
      $color_array = explode(',', $GLOBALS['google'][$param]);
      google_append_url($url, $param,
                        $color_array[$google_dt % count($color_array)]);
    }

    function google_set_screen_res() {
      $screen_res = read_global('HTTP_UA_PIXELS');
      if ($screen_res == '') {
        $screen_res = read_global('HTTP_X_UP_DEVCAP_SCREENPIXELS');
      }
      if ($screen_res == '') {
        $screen_res = read_global('HTTP_X_JPHONE_DISPLAY');
      }
      $res_array = preg_split('/[x,*]/', $screen_res);
      if (count($res_array) == 2) {
        $GLOBALS['google']['u_w']=$res_array[0];
        $GLOBALS['google']['u_h']=$res_array[1];
      }
    }

    function google_set_muid() {
      $muid = read_global('HTTP_X_DCMGUID');
      if ($muid != '') {
        $GLOBALS['google']['muid']=$muid;
         return;
      }
      $muid = read_global('HTTP_X_UP_SUBNO');
      if ($muid != '') {
        $GLOBALS['google']['muid']=$muid;
         return;
      }
      $muid = read_global('HTTP_X_JPHONE_UID');
      if ($muid != '') {
        $GLOBALS['google']['muid']=$muid;
         return;
      }
      $muid = read_global('HTTP_X_EM_UID');
      if ($muid != '') {
        $GLOBALS['google']['muid']=$muid;
         return;
      }
    }

    function google_set_via_and_accept() {
      $ua = read_global('HTTP_USER_AGENT');
      if ($ua == '') {
        $GLOBALS['google']['via']=read_global('HTTP_VIA');
        $GLOBALS['google']['accept']=read_global('HTTP_ACCEPT');
      }
    }

    function google_get_ad_url() {
      $google_ad_url = 'http://pagead2.googlesyndication.com/pagead/ads?';
      google_append_url($google_ad_url, 'dt',
                        round(1000 * array_sum(explode(' ', microtime()))));
      foreach ($GLOBALS['google'] as $param => $value) {
        if (strpos($param, 'color_') === 0) {
          google_append_color($google_ad_url, $param);
        } else if (strpos($param, 'url') === 0) {
          $google_scheme = ($GLOBALS['google']['https'] == 'on')
              ? 'https://' : 'http://';
          google_append_url($google_ad_url, $param,
                            $google_scheme . $GLOBALS['google'][$param]);
        } else {
          google_append_globals($google_ad_url, $param);
        }
      }
      return $google_ad_url;
    }

    $google_ad_handle = @fopen(google_get_ad_url(), 'r');
    if ($google_ad_handle) {
      while (!feof($google_ad_handle)) {
        echo fread($google_ad_handle, 8192);
      }
      fclose($google_ad_handle);
    }

    ?>

Best Answer

Answers

  • Options
    peregrineperegrine MVP
    edited December 2012

    edited

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    fr3em1ndfr3em1nd ✭✭
    edited December 2012

    hi @peregrine , im sure you're quite aware that mobile themes are written in .tpl ofcourse since the tag is < ? php it's not receiving the codes. there's no visible error, the page is just blank.

    i inserted that code above in the .tpl, im sure this topic could help some newbs around who's wondering how to put a google code with that kind of format.

    i got this resolved and just used { literal } codes here { /literal } and just used the < script > codes from google not < ? php tag mobile ads.

    im just wondering if there's a workaround to make < ? Php tags on mobile, this would be a great help. since the size i want is on < ? php tag served by google.

  • Options

    @peregrine as expected you give the topnotch answers here in vanilla forums, thanks.

  • Options

    @peregrine as expected you give the topnotch answers here in vanilla forums, thanks.

    thx. insightful

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options

    Hi fr3em1nd:

    I'm also trying to add a Google Adsense mobile ad to the mobile theme.

    You mentioned above: i got this resolved and just used { literal } codes here { /literal } and just used the < script > codes from google not < ? php tag mobile ads.

    What do you mean? I tried using the Google PHP code but removing the opening and closing PHP tags, instead inserting to open and to close, with { literal } and { /literal } . But it's not working for me. The mobile theme is loading properly, i.e. no Bonk, but the add is not appearing. I've added the code to default.master.tpl in themes\mobile\views between {event name="AfterBody"} and the closing tag, thus I'm expecting the adsense ad to appear at the bottom of the page.

    Thanks for any insight you have!

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited March 2013

    @ObamaCare_Forums

    What he did was insert the Google adsense javascript in the tpl of the theme instead of the php code for google adsense. To be able to add script to a tpl you use

    {literal} Google adsense javascript goes here {/literal} 
    

    You do not ad php code to the tpl. Insert that js where you need it to appear and use css to adjust it.

    Edit:

    Also look in the plugin info if it has 'MobileFriendly'=TRUE if not add it and see if that makes it appear in the mobile. This has worked for some plugins so it might also work for this if it does not have that already .

  • Options

    Hi vrijvlinder:

    Hmmm, Google does not provide javascript as an option when creating Mobile Content Ad Units. They offer 4 options: PHP, CGI/Perl, JSP and ASP. For markup language, the options are 3: xhtml (WAP2.0), wml (WAP1.x) and chtml. I think xhtml (WAP2.0) is correct for Vanilla, ya?

    But after more Adsense research, I see their "normal" content ads, which are provided as javascript, include an ad unit/size called Mobile Banner that is 320x50. So, I created one and placed the javascript between {literal} and {/literal} in the tpl file, before the tag.

    It's working, thank you!

  • Options

    I think JSP is JavaServer Pages and the code that Google supplies assumes the site is running on a Java web server, not LAMP architecture, thus it doesn't work (I tried it). For example, the code supplied by Google is (not straight javascript):

    <%@ page import="java.io.BufferedReader, java.io.InputStreamReader, java.io.IOException, java.io.UnsupportedEncodingException, java.net.URL, java.net.URLEncoder, java.util.ArrayList, java.util.List" %>
    <%!

    private static final String PAGEAD =
    "http://pagead2.googlesyndication.com/pagead/ads?";

    private void googleAppendUrl(StringBuilder url, String param, String value)
    throws UnsupportedEncodingException {
    if (value != null) {
    String encodedValue = URLEncoder.encode(value, "UTF-8");
    url.append("&").append(param).append("=").append(encodedValue);
    }
    }

    private void googleAppendColor(StringBuilder url, String param,
    String value, long random) {
    String[] colorArray = value.split(",");
    url.append("&").append(param).append("=").append(
    colorArray[(int)(random % colorArray.length)]);
    }

    private void googleAppendScreenRes(StringBuilder url, String uaPixels,
    String xUpDevcapScreenpixels, String xJphoneDisplay) {
    String screenRes = uaPixels;
    if (screenRes == null) {
    screenRes = xUpDevcapScreenpixels;
    }
    if (screenRes == null) {
    screenRes = xJphoneDisplay;
    }
    if (screenRes != null) {
    String[] resArray = screenRes.split("[x,*]");
    if (resArray.length == 2) {
    url.append("&u_w=").append(resArray[0]);
    url.append("&u_h=").append(resArray[1]);
    }
    }
    }

    private void googleAppendMuid(StringBuilder url, List muids) {
    for (String muid : muids) {
    if (muid != null) {
    url.append("&muid=").append(muid);
    return;
    }
    }
    }

    private void googleAppendViaAndAccept(StringBuilder url, String via,
    String accept) throws UnsupportedEncodingException {
    googleAppendUrl(url, "via", via);
    googleAppendUrl(url, "accept", accept);
    }

    %>
    <%

    long googleDt = System.currentTimeMillis();
    StringBuilder googleAdUrlStr = new StringBuilder(PAGEAD);
    googleAdUrlStr.append("&client=ca-mb-pub-xxxxxxxxxxxxxxxxx");
    googleAdUrlStr.append("&dt=").append(googleDt);
    googleAppendUrl(googleAdUrlStr, "ip", request.getRemoteAddr());
    googleAdUrlStr.append("&markup=xhtml");
    googleAdUrlStr.append("&output=xhtml");
    googleAppendUrl(googleAdUrlStr, "ref", request.getHeader("Referer"));
    String googleUrl = request.getRequestURL().toString();
    if (request.getQueryString() != null) {
    googleUrl += "?" + request.getQueryString().toString();
    }
    googleAdUrlStr.append("&slotname=xxxxxxxxxx");
    googleAppendUrl(googleAdUrlStr, "url", googleUrl);
    String googleUserAgent = request.getHeader("User-Agent");
    googleAppendUrl(googleAdUrlStr, "useragent", googleUserAgent);
    googleAppendScreenRes(googleAdUrlStr, request.getHeader("UA-pixels"),
    request.getHeader("x-up-devcap-screenpixels"),
    request.getHeader("x-jphone-display"));
    List googleMuids = new ArrayList();
    googleMuids.add(request.getHeader("X-DCMGUID"));
    googleMuids.add(request.getHeader("X-UP-SUBNO"));
    googleMuids.add(request.getHeader("X-JPHONE_UID"));
    googleMuids.add(request.getHeader("X-EM-UID"));
    googleAppendMuid(googleAdUrlStr, googleMuids);
    if (googleUserAgent == null || googleUserAgent.length() == 0) {
    googleAppendViaAndAccept(googleAdUrlStr, request.getHeader("Via"),
    request.getHeader("Accept"));
    }
    try {
    URL googleAdUrl = new URL(googleAdUrlStr.toString());
    BufferedReader reader = new BufferedReader(
    new InputStreamReader(googleAdUrl.openStream(), "AUTO_DETECT"));
    for (String line; (line = reader.readLine()) != null;) {
    out.println(line);
    }
    } catch (IOException e) {}

    %>

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited March 2013

    lol indeed , well at least there was another option and it worked...I edited the super long info about servlets and how this uses xml tags to encapsulate the logic, but it's there in the oracle website. This stuff should run on anything according to the info. But mobile , yea that is a stretch maybe.

Sign In or Register to comment.