Track WordPress Author On AMP Pages In Google Analytics With PageFrog Plugin

You’re a pioneer in the world of AMP and WordPress, but simply tracking pageviews isn’t enough for you. Yes, the Facebook Instant Articles & Google AMP Pages by PageFrog WordPress plugin makes your life easier, but are you really willing to forego your beloved Custom Dimensions in Google Analytics because the functionality isn’t built in? I think not!

In this article, I’ll show you how to send a WordPress post author’s name to Google Analytics as a custom dimension using AMP Analytics variables with the Facebook Instant Articles & Google AMP Pages by PageFrog plugin.

To make this work, we need to:

  • Set up a Custom Dimension called “author” in Google Analytics
  • Edit the PageFrog plugin code to assign the post author’s name to the “author” Custom Dimension in the Google Analytics script

How To Track The WordPress Author As A Custom Dimension In Google Analytics With The PageFrog AMP Plugin For WordPress

  1. Log into Google Analytics, go to the ADMIN section of your account, and click Custom Dimensions under the PROPERTY heading.
    Admin Section Google Analytics For AMP Custom Dimensions
  2. Add a Custom Dimension called author and tap Create. Add Custom Author Dimension Google Analytics
  3. Take note of the index of author on the Custom Dimensions page. That’s how we’ll tell the Analytics code which dimension to assign our author variable to. In my case, author is index 1. Take Note Of Author Index Number
  4. Open the file located at /wp-content/plugins/pagefrog/public/partials/amp-google-analytics-template.php in your favorite editor. By default, the file looks like this:
    <amp-analytics type="googleanalytics">
        <script type="application/json">
            {
                "vars": {
                    "account": "<?php echo $analytics->get_google_analytics_site_id(); ?>"
                },
                "triggers": {
                    "trackPageview" : {
                        "on": "visible",
                        "request": "pageview"
                    }
                }
            }
        </script>
    </amp-analytics>
  5. Get the WordPress post author’s name and send it as an AMP Analytics Variable to Google Analytics as a Custom Dimension by updating the code like this:
    <amp-analytics type="googleanalytics">
    	<script type="application/json">
    	    {
    	        "requests": {
    	            "pageviewWithCd1": "${pageview}&cd1=${cd1}"
    	        },      
    	        "vars": {
    	            "account": "<?php echo $analytics->get_google_analytics_site_id(); ?>"
    	        },
    	        "triggers": {
    	            "trackPageviewWithCustom" : {
    	                "on": "visible",
    	                "request": "pageviewWithCd1",
    	                "vars": {
    	                    "cd1": "<?php global $post; $author_id=$post->post_author; the_author_meta( 'display_name', $author_id ); ?>"
    	                }
    	            }
    	        }
    	    }
    	</script>
    </amp-analytics>

    Important: Replace Cd1 and cd1 with cd(index of your author custom dimension), and be careful of capitalization.

  6. Verify the author’s name is being added to your HTML by opening the inspector in Google Chrome and viewing the Google Analytics code being inserted just after the opening <body> tag.
  7. Verify the AMP code is valid by opening the JavaScript console in Google Chrome and visiting your AMP page with #development=1 appended to the url. If you see “AMP Validation Successful.”, you’re good to go. AMP Validation Successful in Google Chrome

WordPress Author: Identified.

Now that you’re totally AMPed because you’re tracking each individual author’s performance in Google Analytics, congratulate yourself on being one of the probably two or three people who has found this article interesting enough to actually read it. We WordPress AMP pioneers need to stick together, and I’m glad you found the answer you were searching for here. Leave a comment below if it worked. Or if it didn’t.

Thanks for reading and all the best,
David P.

About Author

I'm a former Apple employee and the founder of Payette Forward, and I'm here to help you with your iPhone.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments