I am running a WordPress website with both traditional and AMP pages. Both versions are using GTM, but the AMP version is not able to find my custom variables. The AMP pages are validating, so I think there is an issue with how I am defining my custom variables
I am using the following code to define the variables on AMP pages. Is there anything out of place that I am missing. to see an example of an AMP page visit https://cellculturedish.com/evolution-stem-cell-culture-cell-derived-extracellular-matrices/amp/.
<body>
<!-- Google Tag Manager -->
<amp-analytics config="https://www.googletagmanager.com/amp.json?id=GTM-xxxxxxxxx.url=SOURCE_URL" data-credentials="include">
<?php if ( is_singular() ) { ?>
<?php global $post; ?>
<?php $categories = get_the_category( $post->ID ); ?>
<?php $primary_category = $categories[0]; ?>
<?php $post_sponsorship = get_field('post_sponsorship', $post->ID); ?>
<?php $is_sponsored = $post_sponsorship['is_sponsored']; ?>
<?php $author = 'Multiple Authors'; ?>
<?php $author_options = get_field('author_options', $post->ID); ?>
<?php if ( $author_options == 'ccd' && get_field('ccd_author') ) { ?>
<?php $author_obj = get_field('ccd_author', $post->ID); ?>
<?php $author = get_userdata( $author_obj ); ?>
<?php $author = $author->data->display_name; ?>
<?php } elseif ( $author_options == 'guest_post' && count( get_field('contributing_experts', $post->ID) ) == 1 ) { ?>
<?php $author_array = get_field('contributing_experts', $post->ID); ?>
<?php $author = get_expert_title( $author_array[0] ); ?>
<?php } ?>
<script type="application/json">
{
"vars": {
"publishDate": "<?php echo get_the_date('', $post->ID); ?>",
"postCategory":"<?php echo $primary_category->name; ?>",
"isSponsored": "<?php echo $sponsored = $post_sponsorship['is_sponsored'] == true ? 'true' : 'false'; ?>",
<?php if ( $post_sponsorship['is_sponsored'] == true && $post_sponsorship['sponsor'] != null ) { ?>
"postSponsor": "<?php echo get_the_title( $post_sponsorship['sponsor'] ); ?>",
<?php } ?>
"postAuthor": "<?php echo $author; ?>"
}
}
</script>
<?php } ?>
Create AMP Variables in GTM for all the custom dimensions you have under "vars" like so:
Next go to your GA Tag, under "more settings" > "custom dimensions" add in the correct index number for your custom dimensions and the amp variable name you just created for them: