Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:17:16+00:00 2026-05-31T14:17:16+00:00

I now have this code based on some of the answers below. Is this

  • 0

I now have this code based on some of the answers below.

Is this the most elegant, clean, fast and effective code to have?

<?php
/**
 * The default template for displaying Google Analytics
 *
 * @package WordPress
 * @subpackage News_Template
 * @since News Template 1.0
 */
$googleanalyticscode="<script type='text/javascript'> var _gaq = _gaq || []; _gaq.push(['_setAccount', '%s']); _gaq.push(['_setDomainName', '%s']);   _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>";
$analyticsurlgoogle=array(
    'domain-a.com' => 'UA-25133-2',
    'domain-b.com' => 'UA-25133',
    'domain-c.com' => 'UA-2699-2',
    'domain-d.com' => 'UA-3021-2',
    'domain-e.com' => 'UA-25537-2',
    'domain-f.com' => 'UA-7213-2',
    'domain-g.com' => 'UA-7214-2',
    'domain-h.com' => 'UA-150-2',
    'domain-i.com' => 'UA-150-2'
); 
// --- /Configuration --- 
// Get the Domain URL 
$analyticsurl = get_site_url(); 
$namegoogle = substr($analyticsurl,7); 
//create code 
if (isset($analyticsurlgoogle[$namegoogle])) $code=sprintf($googleanalyticscode,$analyticsurlgoogle[$namegoogle],$namegoogle); 
else $code=''; 
echo $code; ?>

—— Previous Code ——

I have written the following if statement in PHP. What would be the most elegant, effective and cleanest way of writing this code?

The purpose of the code is to check the domain name of the site. If the site have google analytics defined it should match the defined “Google Analytics” code for the domain and then print the script to the page.

If the Google Analytics code is not defined it should show nothing!

<?php 
// Get the Domain URL
$analyticsurl = get_site_url();
// Check if domain is defined
if ($analyticsurl == 'http://domain-a.com') {$analyticcode = 'UA-25133920-1'; $analyticsurlname = 'domain-a.com';} // domain-a.com
if ($analyticsurl == 'http://domain-b.com') {$analyticcode = 'UA-25133920-1'; $analyticsurlname = 'domain-b.com';} // domain-b.com
if ($analyticsurl == 'http://domain-c.com') {$analyticcode = 'UA-26990264-1'; $analyticsurlname = 'domain-c.com';} // domain-c.com
if ($analyticsurl == 'http://domain-d.com') {$analyticcode = 'UA-30217571-1'; $analyticsurlname = 'domain-d.com';} // domain-d.com
if ($analyticsurl == 'http://domain-e.com') {$analyticcode = 'UA-25537388-1'; $analyticsurlname = 'domain-e.com';} // domain-e.com
// if domain is defined create Google Analytics Code and insert variables
$analyticscode_1 = "<script type='text/javascript'>
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '".$analyticcode."']);
  _gaq.push(['_setDomainName', '".$analyticsurlname."']);
  _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>";
// if URL does not match write nothing
if ($analyticcode == '') {$analyticscode = '';}
// if URL exists set the Google Analytics Code
if ($analyticcode != '') {$analyticscode = $analyticscode_1;}
?>
<?php // write the Analytics code to the page
echo $analyticscode ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:17:17+00:00Added an answer on May 31, 2026 at 2:17 pm
    <?php
    
    // --- Configuration ---
    
    $template="<script type ... 
      _gaq.push(['_setAccount', '%s']);
      _gaq.push(['_setDomainName', '%s']);
      ...
    </script>";
    
    $urls=array(
     'domain-a.com' => 'UA-25133920-1',
     ...
     'domain-e.com' => 'UA-25537388-1'
    );
    
    // --- /Configuration ---
    
    // Get the Domain URL
    $url = get_site_url();
    $name = substr($analyticsurl,7);
    
    //create code
    if (isset($urls[$name])) $code=sprintf($template,$urls[$name],$name);
    else $code='';
    
    // ...
    
    echo $code;
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code right now to get the row value from jquery grid..
I have this code in a xaml file now in a event handler code
Right now, I have this code where $obj_arr maybe contain array and an object.
I have implemented this code, but fail at compilation (VC2008 Express Ed.) : Now
Following on from this question I now have code that can attach to a
I have the following code: SPList list = web.Lists[this.ListName]; SPListItem item = list.Items.Add(); now
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
i have been having this issue for some time now, and have not gotten
Right now I have this SQL query which is valid but always times out:
Dear all,Now i have this question in my java program,I think it should be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.