Google Content Experiment code

Hi

I’m setting up a google content experiment, and need to place code directly after the head tag on one of my pages.

How would I insert the code directly after the head tag when using a BB theme?

Thanks!

Hey James,

You can place the code under Appearance > Customize > Code > Head Code. :slight_smile:

Ben

Thanks Ben

I did look at this, but wasn’t sure for the following reasons:

  1. the code needs to be placed on one specific page (the original in the experiment), and when I use customise > code > head code, it appears on the alternative pages in the experiment too

  2. the instructions are very specific in saying that it appears directly under the head tag, and when I use the head code editor, the code appears a fair way down - I don’t know if this is an issue or not. If you say not, then that’s fine

How could I isolate the code just to one page? And if needed, get it higher under the head tag?

Thanks

Hey James,

That section won’t work if you only need it on one page. We’re gonna have to add it via a custom function. The code below should do the trick! Just make sure to change the page ID to the correct one.

function fl_google_code() {
  if( is_page(5723) ) {
    echo 'Code goes here';
  }
}
add_action( 'fl_head' , 'fl_google_code' );

It shouldn’t matter what section in the head tag it is placed, what matters is it’s inside it. :slight_smile:

Ben

Hi

Will give this a try.

A couple of quick questions:

  • page ID - I’ve looked for this on the Pages menu but can’t see the ID anywhere, just the URL /wheelprotectors - what am I missing?

  • what is a customer function? trying to work out where I need to put the code

  • double checking - I put all of my experiment code in the ‘code goes here’ area. Wanted to double check

Sorry for the all the follow ups!

Hey James,

Sorry, I should have added those to my reply. You can check the Youtube video below on how to get your post ID. It is uses a very old version of WP, and is doing it under Posts but it should be similar to Pages.
https://www.youtube.com/watch?v=fLg2T1AvmFE

The code above needs to go to your functions.php file, preferably in a child theme so it doesn’t get overwritten.

Yes, that is correct. Actually, can you share the code you’re trying to use so I can check? You can post it here using the private reply option below if necessary.

Ben

Hi - thanks

I’ve never been near any of the files before! From what I can see, I need to go to Appearance > Editor > select BB Child Theme > click on Theme Functions (functions.php) > then insert all the code you provided at the bottom of the code already in that box.

So the code I’d past would be (I’ve put my page ID in as number 5 and included the experiments code):

function fl_google_code() {
if( is_page(5) ) {
echo ‘<!-- Google Analytics Content Experiment code -->
<script>function utmx_section(){}function utmx(){}(function(){var
k=‘112073461-1’,d=document,l=d.location,c=d.cookie;
if(l.search.indexOf(‘utm_expid=’+k)>0)return;
function f(n){if©{var i=c.indexOf(n+’=’);if(i>-1){var j=c.
indexOf(’;’,i);return escape(c.substring(i+n.length+1,j<0?c.
length:j))}}}var x=f(’__utmx’),xx=f(’__utmxx’),h=l.hash;d.write(
‘<sc’+‘ript src="’+‘http’+(l.protocol==‘https:’?‘s://ssl’:
‘://www’)+’.google-analytics.com/ga_exp.js?’+‘utmxkey=’+k+
‘&utmx=’+(x?x:’’)+’&utmxx=’+(xx?xx:’’)+’&utmxtime=’+new Date().
valueOf()+(h?’&utmxhash=’+escape(h.substr(1)):’’)+
‘" type=“text/javascript” charset=“utf-8”></sc’+‘ript>’)})();
</script><script>utmx(‘url’,‘A/B’);</script>
<!-- End of Google Analytics Content Experiment code -->
';
}
}
add_action( ‘fl_head’ , ‘fl_google_code’ );

Hey James,

You need to use the Child Theme so this works.

Since the Google code is using single quotes as well, the code above might not work. But the one below should.

function fl_google_code() {
  if( is_page(5) ) { ?>
    Code here
  <?php }
}
add_action( 'fl_head' , 'fl_google_code' );

Just take note pasting the code elsewhere might convert the quotes to different formats, rendering the code useless. I’d suggest this workflow:

  1. Paste the code above to your functions.php file directly.
  2. Copy the Google code from the Google site directly
  3. Go back to the functions file, delete the code here part, and paste the code

Let us know how it goes! :slight_smile:

Ben

Hey James,

After I sent my reply above, I tried looking for easier ways to do this and found the plugin below. I haven’t tried it yet but you could give it a shot. :slight_smile:
https://wordpress.org/plugins/per-page-add-to/

Ben

Hey

Thanks for this!

It’s been an interesting couple of hours.

I found another plug in called google content experiments that looked easy to setup but it needed code just below the head tag so I put their code in the functions.php file. This was a big mistake. Site went down. So used FTP for first time to reverse the error. Good learning!

Before I tried the original code you provided, I saw your per page pluging. Tried it and it seems to work!! I’ve not had any results in google analytics yet, but the initial validation worked. So am going to try using it.

Thanks for your help! :slight_smile:

I’ve got the experiment up and running now, and my site is alternating between the 3 variants, so the plug in did work. Thanks again

Hi James,

Happy to hear your issue has been resolved and thank you for informing us, we appreciate it.

Thanks,
Danny