Search code examples
phpwordpressgoogle-analyticspixel

Embedding Google Conversion Code in Alert


I'm using Avada's template (http://theme-fusion.com/avada/contact/) and working with their contact page. Whenever someone submit's a successful form, the page is being reloaded with an additional "alert" box that's populated with a success message. I'm looking to embed a Google Conversion Pixel in this alert box if possible instead of having to send the user to a whole new page. The code that creates the alert box is

<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>

<?php echo do_shortcode( '[alert type="success" accent_color="" background_color="" border_size="1px" icon="" box_shadow="yes" animation_type="0" animation_direction="down" animation_speed="0.1" class="" id=""]' .  __( 'Thank you', 'Avada' ) . ' <strong>' . $name . '</strong> ' . __( 'for contacting us! Your email was successfully sent!', 'Avada' ) . '[/alert]' ); ?>                                   
<br />
<?php } ?>

which generates

<div class="fusion-alert alert success alert-dismissable alert-success alert-shadow">
    <button type="button" class="close toggle-alert" data-dismiss="alert" aria-hidden="true">&times;</button>
    <span class="alert-icon">
        <i class="fa fa-lg fa-check-circle"></i>
    </span>
    Thank you <strong>Test</strong> for contacting us! Your email was successfully sent!</div>                              
    <br />
</div>

Code looking to embed looks like this

<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 1;
var google_conversion_language = "en";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_label = "1";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt=""    src="//www.googleadservices.com/pagead/conversion/1/?label=1&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

Any help is greatly appreciated


Solution

  • Since each line has opening and closing php tags you can simply paste the html code between two php blocks (but after the "if" block). You might choose to just use the image tag from the noscript block (conversions will still be tracked).

    <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
    // just the noscript image tag, alternatively paste the whole code here
    <img height="1" width="1" style="border-style:none;" alt=""    src="//www.googleadservices.com/pagead/conversion/1/?label=1&amp;guid=ON&amp;script=0"/>
    <?php 
    echo do_shortcode( '[alert type="success" accent_color="" background_color="" border_size="1px" icon="" box_shadow="yes" animation_type="0" animation_direction="down" animation_speed="0.1" class="" id=""]' .  __( 'Thank you', 'Avada' ) . ' <strong>' . $name . '</strong> ' . __( 'for contacting us! Your email was successfully sent!', 'Avada' ) . '[/alert]' ); ?>                                   
    <br />
    <?php } ?>