Jump to content

Recommended Posts

Hi all,

My company has a php site on wordpress that i’m trying to add google AD tracking to.
There is a single header.php file for all pages so i’ve added a little piece of php to check what the current page is and to write out the appropriate event trigger.

Now, on the live site, when i’m logged into word-press and run the page, I can see the triggers firing fine on Tag assistant, if i un-comment the alerts, they also fire.

When i log out of word-press and hit the page, nothing happens at all. No alerts, no google triggers, nothing in the php is running?

Any ideas whats going on ?

 

<script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'AW-1234567897'); <?php $url = $_SERVER['REQUEST_URI']; if ($url =="/thankyou/") { print " gtag('event', 'conversion', {'send_to': 'AW-1234567897/1234567897', 'value': 1.0, 'currency': 'GBP' }); "; print " gtag('event', 'conversion', {'send_to': 'AW-1234567897/1234567897'}); "; //Print "alert('OK');"; //Print "alert('" . $url . "');"; } ?> </script>

tia

Dave

Thanks for the reply requinix,

no. i cant see the google JS as its hidden in source.

However, as a test,  at the top of the header.php i added this code.

<%php
Print “alert(‘hello’);”;
?>

 

when i’m logged into WP and hit the page, it triggers fine,  (and i can see this in sourcecode)

But if i log out of WP it doesn’t ( and not visible).
just something very weird going on here i think.

 

 

 

 

 

 

If you don't see it in the source then you added the code to the wrong template. Or you accidentally put it inside some block that's only outputted for logged-in users.

Either way, needs to go somewhere else.

thanks for the replies, 

interesting that there's outputted blocks for admin or users, i'll have to look into that.

 

its the header.php file. should be present on every page,in fact it is.. 

The rest of the JS is coming out OK , such as the config tag,  the initial async line  etc..

its just the PHP code isnt showing ?

 <script async src="https://www.googletagmanager.com/gtag/js?id=AW-123123123"></script> 
  <script> 
    window.dataLayer = window.dataLayer || []; 
    function gtag(){dataLayer.push(arguments);} 
    gtag('js', new Date()); 
    gtag('config', 'AW-123123123');         
    <?php
    $url = $_SERVER['REQUEST_URI'];
    if ($url =="/thankyou/") {
            print " gtag('event', 'conversion', {'send_to': 'AW-123123123/abcabcabcabc', 'value': 1.0, 'currency': 'GBP' }); ";
            print " gtag('event', 'conversion', {'send_to': 'AW-1123123123/asasasasasasasas'}); ";
            //Print "alert('OK');";
            //Print "alert('" . $url . "');";
        }            
    ?>
  </script>

 

very strange.

 

 

5 hours ago, davidhb21l6 said:

its just the PHP code isnt showing ?

Try adding a test line before your if statement. For example:

<?php
$url = $_SERVER['REQUEST_URI'];


print 'HERE';


if ($url =="/thankyou/") {

Does "HERE" appear on your pages when you're logged out...and in? For what it's worth, the PHP code in your if statement only gets executed when you're on the "/thankyou/" page. Is that page only displayed when you are logged in?

Edited by cyberRobot
added clarification
18 hours ago, davidhb21l6 said:

interesting that there's outputted blocks for admin or users, i'll have to look into that.

 

its the header.php file. should be present on every page,in fact it is.. 

The rest of the JS is coming out OK , such as the config tag,  the initial async line  etc..

Depending on what theme you're using, it's very possible that that exact code is duplicated across multiple templates, so you could be in the wrong file without knowing it. WordPress doesn't exactly require - or even in a lot of ways promote - good practices when it comes to PHP and coding for the web. And some of the Automattic plugins are the worst offenders (WooCommerce, I'm looking at you), so don't assume it's good just because it came from the same company as WP or it's got a lot of stars.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.