Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Replace Google Analytics with other type

My customer has been using Google Analytics in all the programs in several apps for a long time. The code is like this:


<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-PE5G5Y55LG"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-PE5G5Y55LG');
</script>

They want to replace this with another form of Analytics. I use Text Crawler (V3), Pro, to do search & replace in text files (Windows 10). I pasted in the code above into the "Search" line & the replacement into the "Replace" line. It only replaces the first line  (<!-- Global......) with the other text. I notice there is a "Regular Expression" option, I don't know what that means in this context. Would that do multiple lines?


In doing a search it looks like I MIGHT be able to do this in Notepad++, which I use all the time, but it's not clear how. I want to do MULTIPLE files, find text block A, replace with text block B.


Is there a way to do that in Text Crawler or do I have to use something else. If something else, what is it. I realize I could write a program in php to do this, prefer not to have to do that.


Thank you

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

How many sites are there? It would probably faster to just go to 10 sites and replace the code manually in each of the 10 spots then it would be to figure not only how to program this, IF the code is an exact match but you also have different id's (one for each site) and you may have different versions of the GA code that you are using.  
Avatar of Richard Korts

ASKER

The code is an exact match. There are around 200 programs in one app, maybe half that in the other, Can it be done (over multiple files) in Notepad++? That's what I use for writing & editing code.

Thanks
I am home on my mac right now, but I am pretty sure notepad++ has this feature and I just looked it up https://darrenoneill.eu/?p=1250 so you can do this.

I use PHP storm for my sites and PHP storm has this feature and that is what I have been using for search and replace in multiple files. 
Does your match scour the multi line.
Personal I would look whether a centralized file

Have a local file as
Analyitics.js
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-PE5G5Y55LG"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-PE5G5Y55LG');
</script>

Open in new window


Test whether that wouldwork
This way shoukd you need tochange,
Single file thatis all it takes.

To avoid this scenario in the future.
Scott, that does not address how to replace a multi-line text block with another.

I use Notepad++ all the time to do the simple stuff, like the example shows.

But I figured another way, there are only 8 lines in the Google block (not counting blank lines). I can erase those one at a time (8 passes) & then replace </head> with the new (one line) link followed by </head>

Richard
What Arnold says is the way it "should" have been done & I will do that for the new one.

Richard
text type things I'd use perl
and actually create a template
<!-- analytic start -->


<!-- analytic end -->
whether the above is a single block of code or a reference to a file ...
Unfortunately I can’t test it but I think if you copied and pasted the multi line would work test it and one file and see what happens
Scott,

That is EXACTLY what I did originally. It worked. So I assumed it would work for all.

It didn't.

Richard
have all of the files loaded into notepad++ and do your search and replace in all opened documents
(remember to save the changes)
David, sounds like a possibility.

I'll try it.

Richard
What form of Analytics is the replacement?   I ask because "Google Tag Manager (gtm.js)" is Not a dropin replacement for "Global site tag (gtag.js)".
ASKER CERTIFIED SOLUTION
Avatar of Richard Korts
Richard Korts
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial