WPMetronet / metronet-tag-manager

Integrate Google Tag Manager into your website. Use dataLayer variables on a per-post and per-page basis. Use variables for flexibility.
5 stars 8 forks source link

Preserve data structures #1

Closed lkraav closed 8 years ago

lkraav commented 8 years ago

1.0.6

Arrays and objects don't currently work. preg_replace_callback() isn't going cut it in this scenario, where non-string data needs to go out, because $value coming in from apply_filters( "gtm_" ) will be set to Array.

Just return an array from any of your variable filters to reproduce this.

Take a look at https://github.com/lkraav/metronet-tag-manager/commit/09d6202a854a9380a4775674b6f96ac809701864

It's minimally invasive and does the job, but I don't know all the use cases that preg_replace_callback() is supposed to handle - whether a simple str_replace() is enough or not.

Bad output (check user_role key at the end):

dataLayer = [{"title":"What is brand love and how does it evolve over time?","author":"Maddie","wordcount":145,"logged_in":"true","page_id":3027,"post_date":"2016-03-18 18:32:01","post_type":"insight","user_role":"Array"}];</script>

Correct example output:

dataLayer = [{"title":"What is brand love and how does it evolve over time?","author":"Maddie","wordcount":145,"logged_in":"true","page_id":3027,"post_date":"2016-03-18 18:32:01","post_type":"insight","user_role":["administrator"]}];</script>
ronalfy commented 8 years ago

Understood. I'll do some testing and try to reproduce.

ronalfy commented 8 years ago

I decided to go with this: https://github.com/ronalfy/metronet-tag-manager/blob/master/metronet-tag-manager.php#L326

If you have time, please give it a go and I'll try to push out a release this evening.

lkraav commented 8 years ago

This lgtm and works correctly, thanks.

For the release, perhaps you also want to pick up this https://github.com/lkraav/metronet-tag-manager/commit/59d97ea1ba429ba45ce27ca2a02ae5794e700cc8

ronalfy commented 8 years ago

Had to change to sprintf to allow for the new line, but this seems to be working: https://github.com/ronalfy/metronet-tag-manager/blob/master/metronet-tag-manager.php#L341

Going to close out the issue. I'll push out a release tonight.