mercredi 12 mai 2021

How do I create a new variable inside of an if else? [closed]

I'm trying to create a "dynamic" script to distribute a facebook pixel and a GTM among different pages. To do so, I'm using an if else statement. Tried a couple variations and it seems to not be working for some reason. The script always uses the last "else" gtm/pixel id (the error one).

Edit: All the data in the $pixel_id/$gtm_id is sample data, real data is not the same.

Page1.php:

if ($page == "one") 
{
    $pixel_id = "123456"; // FB pixel
    $gtm_id = "GTM-123456"; // GTM Tag
}
elseif ($page == "two") 
{
    $pixel_id = "78910";
    $gtm_id = "GTM-78910";
}
else 
{
    $pixel_id = "error";
    $gtm_id = "GTM-error";
}
(...)
        <!-- Facebook Pixel Code -->
        <script>
        !function(f,b,e,v,n,t,s)
        {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
        n.callMethod.apply(n,arguments):n.queue.push(arguments)};
        if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
        n.queue=[];t=b.createElement(e);t.async=!0;
        t.src=v;s=b.getElementsByTagName(e)[0];
        s.parentNode.insertBefore(t,s)}(window,document,'script',
        'https://connect.facebook.net/en_US/fbevents.js');
        fbq('dataProcessingOptions', ['LDU'], 0, 0);
        fbq('init', '<?php echo $pixel_id; ?>');
        fbq('track', 'PageView');
        
        </script>
        <noscript>
        <img height="1" width="1"
        src="https://www.facebook.com/tr?id=<?php echo $pixel_id; ?>&ev=PageView(44 B)
        https://www.facebook.com/tr?id=<?php echo $pixel_id; ?>&ev=PageView
        &noscript=1"/>
        </noscript>
        <!-- End Facebook Pixel Code --> 

        <!-- Google Tag Manager -->
        <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
        'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer','<?php echo $gtm_id; ?>');</script>
        <!-- End Google Tag Manager -->
        <!-- Google Tag Manager (noscript) -->
        <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?php echo $gtm_id; ?>"
        height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
        <!-- End Google Tag Manager (noscript) -->

Page2.php

<?php
$page = "one";
require("../page1.php");
?>

Any ideas why? Thinking that maybe I'm not declaring the variables correctly or something like that. Any help would be appreciated, sorry for the noob question.

Aucun commentaire:

Enregistrer un commentaire