Support » Plugin: Web Stories » Adding a tracking script
Adding a tracking script
-
How do you get a tracking script for a third party into the header on these? I’ve tried several things and nothing has worked so far. I normally can set a global one via a tracking manager plugin. And it will show on all posts and pages. I’m assuming since this is a special post type it is scripting all of that out?
It’s worked great so far except that.
Help is needed, thanks.
The page I need help with: [log in to see the link]
-
Hi there,
Which tracking scripts and tracking manager plugins are you typically using?
Since Web Stories rely on the underlying AMP format, printing of tracking scripts works a bit differently from regular pages as they need to be valid AMP.
For example, to print a Facebook pixel you’d need to use
<amp-pixel src="https://www.facebook.com/tr?id=<ID>ev=PageView&noscript=1" layout="nodisplay"></amp-pixel>
at the right place, before the closing</amp-story>
tag.You can do so by hooking into the
web_stories_print_analytics
action.A simple example would look like this:
add_action( 'web_stories_print_analytics', function() { echo '<amp-pixel src="https://www.facebook.com/tr?id=<ID>ev=PageView&noscript=1" layout="nodisplay"></amp-pixel>'; } );
- This reply was modified 3 years, 5 months ago by Pascal Birchler. Reason: fixed code formatting
So this is the script for our Ad company –
<script async src='https://tag.simpli.fi/sifitag/9a3b1250-6036-0136-ee82-06659b33d47c'></script>
I’m trying to use these as landing pages for the ads. Since a lot of this traffic is mobile this presents a very nice format – I think for mobile.So I normally use Tracking Script Manager from https://wordpress.org/plugins/tracking-script-manager/
I’ve tried with Google Tag Manager as well but it shows everywhere but the Web Stories. However, I’m not very familiar with it yet, so I could be doing something wrong there.
- This reply was modified 3 years, 5 months ago by keithasmith.
So would you add your example to the Tracking.php or Analytics.php file? Or somewhere else?
Thanks for your help.
- This reply was modified 3 years, 5 months ago by keithasmith.
Neither!
That example would belong into your theme’s
functions.php
file or a dedicated custom plugin. You should not modify the Web Stories plugin files as these changes get overridden with any update.Example:
add_action( ‘web_stories_print_analytics’, function() { echo ‘<amp-pixel src="https://tag.simpli.fi/sifitag/9a3b1250-6036-0136-ee82-06659b33d47c" layout="nodisplay"></amp-pixel>’; } );
Note: we’ll be looking into how we can best support plugins like Tracking Script Manager so you don’t have to deal with this manually.
Ah ok, thanks! I did try that earlier but it was without the web-storeies_print_anlytics action. Trying that out now. Everyone really liked how these stories turned out btw.
You all are doing some nice work with this.
Is there a way to support mailto links and tel links?
- This reply was modified 3 years, 5 months ago by keithasmith.
Ok, the echo line is getting this error – syntax error, unexpected ‘src’ (T_STRING), expecting ‘;’ or ‘,’
Ok figured it out, I think. it needed the straight quote marks. Now to check it!
Well, it’s working for everything else but those pages looks like. 🙁
add_action( 'web_stories_print_analytics', function() { echo '<amp-pixel src="https://tag.simpli.fi/sifitag/9a3b1250-6036-0136-ee82-06659b33d47c" layout="nodisplay"></amp-pixel>'; } ); add_action('wp-footer', 'web_stories_print_analytics');
Ok, I got this to work using this. It is showing up. But I think I need it in the header.
@keithasmith Hi there! Glad it is working for you. Please let us know if there is anything further we can help with on this issue. Thanks.
Ok, ran into a slight issue. The tag won’t fire with double quotes (” tag “) when I try single quotes php doesn’t like it. Is there a way around this? The tag needs to display like this:
<script async src='https://tag.simpli.fi/sifitag/9a3b1250-6036-0136-ee82-06659b33d47c'></script>
I get this error when I try – Your PHP code changes were rolled back due to an error on line 113 of file wp-content/themes/ewc-2012/functions.php. Please fix and try saving again.
syntax error, unexpected ‘https’ (T_STRING), expecting ‘;’ or ‘,’
@keithasmith Thanks for the update. We will investigate further and provide you with any updates.
@keithasmith Quotes should really not make a difference. Note that you cannot use
<script>
for Web Stories pages as it will cause your story to become invalid AMP.The code I shared with you is correct (after fixing the quotes) and it is correct to be in the footer.
@keithasmith We are going to mark your topic as resolved as we haven’t received a response. Please feel free to open a new support topic if you continue to experience any issues. Thank you!
- The topic ‘Adding a tracking script’ is closed to new replies.