• Following up on this previous thread about the AMP support in AIWP, which works great for me now!

    https://wordpress.org/support/topic/amp-in-ga4-does-not-working/#post-16273236

    The last thing I needed was to be able to add a second tracker alongside the one that AIWP configures. I use two trackers so that one can track the individual site, while the second tracks a whole network of sites together. I don’t think it’s something AIWP should support with the UI, because it’s rarely needed and would make things much more complex for all users, but I think it’s good for AIWP to support it indirectly through filters.

    I figured out how to do it, and wanted to share the code for the sake of anyone else attempting the same thing, and also for the developer Alin, so you can see how I did it and consider possibly making it easier to do with filters. This solution works, but it’s very fragile and reliant on the exact way the AIWP code works.

    Here is the code in a Gist: https://gist.github.com/jerclarke/2b904e2d91b85e162a5115c8529a80d6

    Basically I just create a second copy of AIWP_Tracking_GA4_AMP, which duplicates the hooks and causes a second copy of the JS to be inserted into each AMP page.

    Then I filter the $config for that second object to insert my new tracking code in two different places.

    The biggest problem is there’s no way to access the ->measurement_id from the outside of the object. It’s protected and automatically populated from the AIWP settings. My only option I could think of is to first add a special flag on the object when creating it, then later filter $config and if the flag is there, manually replace the measurement_id in two different places.

    As you can see in the code, it makes quite a complicated setup, and one that could easily break if AIWP changed.

    My dream would be some kind of API/action that let me manually declare a second tracker to be used with AIWP. Ideally it would then duplicate both this and any other trackers, like the main gtag() one, which I handle elsewhere in my code with a whole other hack!

    Short of that, it would be nice if I could just pass a measurement_id into AIWP_Tracking_GA4_AMP to override the one from AIWP settings. If I could do that, this whole setup would just be the first method that creates the extra copy of AIWP_Tracking_GA4_AMP, i.e. :

    
    $my_aiwp_amp = new AIWP_Tracking_GA4_AMP($tracking_code);
    

    I know I’m asking for a lot, and I don’t expect any of it! I’ve got a working solution now so I’m happy and can move forward, just wanted to document my thoughts here for the sake of myself in the future, and anyone else interested in what I found!

    Thanks so much Alin for your work on this plugin! I’ve been amazed at how helpful you’ve been this week and I have huge confidence in the quality and value of AIWP going forward!

    🙏🏻🙏🏻🙏🏻

Viewing 1 replies (of 1 total)
  • Plugin Author Alin Marcu

    (@deconf)

    At some point, we could apply the full encapsulation principle to that class, so it would be easier to manipulate it’s properties. There are no security concerns or stuff like that to have it in this current form.

Viewing 1 replies (of 1 total)
  • The topic ‘Adding a second tracker for AMP tracking in AIWP’ is closed to new replies.